Delegate in iOS - iOS Tutorial |

Process Advisors

*Subject to Terms and Condition

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.
The delegate may respond to the message by updating the appearance or state of itself or other objects in the application and in some cases it can return a value that affects how an imminent event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.

Steps to Creating a Delegate
Step 1. First, create a single view application.
Step 2. Then select File -> New -> File…
Step 3. Then select Objective C Class and click Next.
Step 4. Provide the name to the class like IntellipaatProtocol with subclass as NSObject
Step 5. Then select create.
Step 6. Add a protocol to the IntellipaatProtocol.h file and the updated code is as follows –

#import <Foundation/Foundation.h>
// Protocol definition starts here
@protocol IntellipaatProtocolDelegate <NSObject>
- (void) processCompleted;
// Protocol Definition ends here
@interface IntellipaatProtocol : NSObject
// Delegate to respond back
id <IntellipaatProtocolDelegate> _delegate;
@property (nonatomic,strong) id delegate;
-(void)startIntellipaatProcess; // Instance method

Step 7. Implement the instance method by updating the IntellipaatProtocol.m file as shown below –

#import "IntellipaatProtocol.h"
@implementation IntellipaatProtocol
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self.delegate
selector:@selector(processCompleted) userInfo:nil repeats:NO];

Step 8. Insert a UILabel in ViewController.xib through dragging label from object library to UIView.

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

#import <UIKit/UIKit.h>
#import "IntellipaatProtocol.h"
@interface ViewController : UIViewController<IntellipaatProtocolDelegate>
IBOutlet UILabel *myLabel;

Learn about audio and video in iOS in our blog on iOS Audio and Video.

Step 10. Implement the delegate method, create object for IntellipaatProtocol and call the startIntellipaatProcess method. The Updated ViewController.m file is as follows –

#import "ViewController.h"
@interface ViewController ()
@implementation ViewController
[super viewDidLoad];
IntellipaatProtocol *intellipaatProtocol = [[IntellipaatProtocol alloc]init];
intellipaatProtocol.delegate = self;
[myLabel setText:@"Operating..."];
[intellipaatProtocol startIntellipaatProcess];
// 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:@"Operation Completed "];

Step 11.  Initially the label displays “Operating…” which gets updated once the delegate method is called by the IntellipaatProtocol object.

Read these Top iOS Interview Questions now that help you grab high-paying jobs!

Course Schedule

Name Date Details
iOS Development Training 30 Sep 2023(Sat-Sun) Weekend Batch
View Details
iOS Development Training 07 Oct 2023(Sat-Sun) Weekend Batch
View Details
iOS Development Training 14 Oct 2023(Sat-Sun) Weekend Batch
View Details

Leave a Reply

Your email address will not be published. Required fields are marked *