Archive for August, 2011
I’ve recently been playing around with delegates and protocols and figured, it might be a good idea to create a simple example. The following article will show you, how you could implement a custom delegate into your own XCode project.
First, why would you want to do this? Well, there are a number of reasons. For instance, picture this: You have a class, that takes care of some calculations. Depending on how complicated these are, they can take up more or less time. Once the calculations are done, you want to send a feedback message to the class, that triggered the process. Using a custom delegate will allow you to receive that message without implementing the calling class into your calculations class. The big advantage is, that you will be able to use your calculations class in any class, send feedback message through the delegate and keep your code nice and clean.
Let’s get started, shall we?
In my sample project, I start off with a “View-based Application” template. It will give me the basic view hierarchy I need and I can get right to the fun part
I create a new “Objective-C” class (subclass of NSObject), name it “DelegateClass” and add it to my project. I now got two new files: DelegateClass.h and DelegateClass.m. Now I can start creating my delegate right away.
First, I want to add a protocol for my delegate. The protocol will contain the feedback method:
@protocol MyCustomDelegate <NSObject> - (void)delegateSentFeedback; @end
Now, I create an instance of my delegate inside @interface:
@interface DelegateClass : NSObject {
id<MyCustomDelegate> delegate; //create instance
}
I also need a method, that will do something. Therefore I name it “doSomething”:
- (void)doSomething;
Finally, I add @property for the delegate, so it can be assigned from any class:
@property (assign) id<MyCustomDelegate> delegate; //@property, so delegate can be assigned from any class #importing "MyCustomDelegate.h"
Inside DelegateClass.m I need to do two things:
Synthesize the delegate:
@synthesize delegate;
and add the doSomething method:
- (void)doSomething {
if (delegate != nil && [delegate respondsToSelector:@selector(delegateSentFeedback)]) { //only trigger method IF delegate has been assigned and the method has been implemented into the target class
//do something and then send callback message
for (int i = 0; i < 10000; i++) {
printf("I'm busy, I'm doing something");
}
[delegate performSelector:@selector(delegateSentFeedback)];
}
}
The IF-Statement will make sure, that the delegate has been assigned from the calling class and that the calling class will implement the feedback method, named “delegateSentFeedback”.
Now, I move over to my viewController. I add a UIButton to start some process upon “TouchUpInside”. I connect it to an IBAction and end up with something like this:
- (IBAction)startProcessAction:(id)sender {
DelegateClass *class = [[DelegateClass alloc] init];
class.delegate = self; //IMPORTANT: if you don't assign the delegate, the feedback method will NOT be triggered!
[class doSomething]; //start process
[class release];
}
Btw, let’s not forget to #import “DelegateClass.h” into our ViewController’s .h file!
Last, but not least I implement the delegate feedback method, defined inside DelegateClass.h
- (void)delegateSentFeedback {
//the process is done. let's show a simple UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"The process is done! Delegate has successfully sent feedback" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
The feedback method will show an UIAlertView, once “doSomething” method inside DelegateClass.m has reached the bottom.
Piece of cake, right?
Feel welcome to grab the sample project here, play with it, add new feedback methods and most importantly HAVE FUN!
