Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom delegates in Objective-C

I need to know about the usage of delegate methods in Objective-C. Can anyone point me to the correct source?

like image 623
Sreelal Avatar asked Mar 14 '09 05:03

Sreelal


People also ask

What is delegate in iOS Objective C?

A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program. The delegating object is often a responder object—that is, an object inheriting from NSResponder in AppKit or UIResponder in UIKit—that is responding to a user event.

How do you write a protocol in Objective C?

Protocols are implemented in the classes conforming to the protocol. A simple example would be a network URL handling class, it will have a protocol with methods like processCompleted delegate method that intimates the calling class once the network URL fetching operation is over. A syntax of protocol is shown below.

What are delegates iOS?

What is delegate methods in iOS? It is an easy and influential pattern in which one object in a program works on behalf of or in coordination with, another object. The delegating object keeps a reference to the other object and at the suitable time sends a message to it.


2 Answers

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.

like image 137
Jonathan Sterling Avatar answered Sep 21 '22 21:09

Jonathan Sterling


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];  } 
like image 30
iCrazyDev Avatar answered Sep 22 '22 21:09

iCrazyDev