How to use custom delegates in Objective-C How to use custom delegates in Objective-C objective-c objective-c

How to use custom delegates in Objective-C


You will want to declare a delegate protocol for your class. An example of a delegate protocol and interface for class Foo might look like this:

@class Foo;@protocol FooDelegate <NSObject>@optional- (BOOL)foo:(Foo *)foo willDoSomethingAnimated:(BOOL)flag;- (void)foo:(Foo *)foo didDoSomethingAnimated:(BOOL)flag;@end@interface Foo : NSObject {     NSString *bar;     id <FooDelegate> delegate;}@property (nonatomic, retain) NSString *bar;@property (nonatomic, assign) id <FooDelegate> delegate;- (void)someAction;@end

Don't forget to synthesize your properties in the @implementation.

What this code did was declare a protocol called FooDelegate; a class that conforms to this protocol would be declared like @interface SomeClass : SuperClass <FooDelegate> {}. Because this class conforms to the protocol FooDelegate, it now gets to implement the methods under FooDelegate (to require that these be implemented, use @required instead of @optional). The last step is for a Foo object to be instantiated in the class that conforms to FooDelegate, and for this Foo object to have its delegate property set:

Foo *obj = [[Foo alloc] init];[obj setDelegate:self];

Now, your class is prepared to receive messages from Foo objects that have their delegates set correctly.


Delegates are very useful to control transfer within the array of view controllers in app manually. Using delegates you can manage the control flow very well.

here is small example of own delegates....

  1. Create a protocol class.... (.h only)

SampleDelegate.h

#import@protocol SampleDelegate@optional#pragma Home Delegate-(NSString *)getViewName;@end
  1. Import above protocol class in the class whom you want to make delegate of another class. Here in my ex. I m using AppDelegate to make delegate of The HomeViewController's Object.

also add above DelegateName in Delegate Reference < >

ownDelegateAppDelegate.h

#import "SampleDelegate.h"@interface ownDelegateAppDelegate : NSObject <UIApplicationDelegate, SampleDelegate>{}

ownDelegateAppDelegate.m

//setDelegate of the HomeViewController's object as[homeViewControllerObject setDelegate:self];//add this delegate method definition-(NSString *)getViewName{    return @"Delegate Called";}

HomeViewController.h

#import#import "SampleDelegate.h"@interface HomeViewController : UIViewController {    id<SampleDelegate>delegate;}@property(readwrite , assign) id<SampleDelegate>delegate;@end

HomeViewController.h

- (void)viewDidAppear:(BOOL)animated {    [super viewDidAppear:animated];    UILabel *lblTitle = [[UILabel alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    lblTitle.text = [delegate getViewName];    lblTitle.textAlignment = UITextAlignmentCenter;    [self.view addSubview:lblTitle];}


If the object(s) in question has its delegate assigned to a class you wrote, say a controller then the methods defined for being that object's class's delegate methods must be implemented by the assigned class. This allows you to effectively control the behavior of the object without sub-classing the object's class in order to override behavior that would likely necessitate an amount of duplicating behavior. It's one of the cleaner parts of the cocoa touch design.

This is something you should pick up in the first couple of intros and tutorials to cocoa touch. Like this tutorial from Cocoa is my Girlfriend. In fact they made the delegate explanation a big bold heading.