Delegates in iOS

Example for delegate

Let’s assume an object A calls object B to perform an action, once the action is complete object A should know that B has completed the task and take necessary action. This is achieved with the help of delegates.

The key concepts in the above example are,

  • A is delegate object of B

  • B will have a reference of A

  • A will implement the delegate methods of B.

  • B will notify A through the delegate methods.

Steps in creating a delegate

1. First, create a single view application.

2. Then select File -> New -> File…

iOS Tutorial

3. Then select Objective C Class and click Next.

4. Give the name for the class say SampleProtocol with subclass as NSObject as shown below.

iOS Tutorial

5. Then select create.

6. Add a protocol to the SampleProtocol.h file and updated code is as follows.

#import <Foundation/Foundation.h>
// Protocol definition starts here 
@protocol SampleProtocolDelegate <NSObject>
- (void) processCompleted;
// Protocol Definition ends here
@interface SampleProtocol : NSObject

   // Delegate to respond back
   id <SampleProtocolDelegate> _delegate; 

@property (nonatomic,strong) id delegate;

-(void)startSampleProcess; // Instance method


7. Implement the instance method by updating the SampleProtocol.m file as shown below.

#import "SampleProtocol.h"

@implementation SampleProtocol

    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self.delegate 
	selector:@selector(processCompleted) userInfo:nil repeats:NO];

8. Add an UILabel in the ViewController.xib by dragging the label from the object library to UIView as shown below.

iOS Tutorial

9. Create an IBOutlet for the label and name it as myLabel and update the code as follow to adopt SampleProtocolDelegate in ViewController.h

#import <UIKit/UIKit.h>
#import "SampleProtocol.h"

@interface ViewController : UIViewController<SampleProtocolDelegate>
    IBOutlet UILabel *myLabel;

10. Implement the delegate method, create object for SampleProtocol and call the startSampleProcess method. The Updated ViewController.m file is as follows.

#import "ViewController.h"

@interface ViewController ()


@implementation ViewController

- (void)viewDidLoad
    [super viewDidLoad];
    SampleProtocol *sampleProtocol = [[SampleProtocol alloc]init];
    sampleProtocol.delegate = self;
    [myLabel setText:@"Processing..."];
    [sampleProtocol startSampleProcess];
	// Do any additional setup after loading the view, typically from a nib.

- (void)didReceiveMemoryWarning
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

#pragma mark - Sample protocol delegate
    [myLabel setText:@"Process Completed"];


11. We will see an output as follows, initially the label will be processing which gets updated once the delegate method is called by the SampleProtocol object.

iOS Tutorial



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: