• Articles
  • Tutorials
  • Interview Questions

Delegate in iOS

Tutorial Playlist

In this part of the IOS tutorial you will learn how to delegate in IOS, steps to creating delegate in IOS and more.

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.
i31

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>
@required
- (void) processCompleted;
@end
// Protocol Definition ends here
@interface IntellipaatProtocol : NSObject
{
// Delegate to respond back
id <IntellipaatProtocolDelegate> _delegate;
}
@property (nonatomic,strong) id delegate;
-(void)startIntellipaatProcess; // Instance method
@end

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

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

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;
}
@end

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 ()
@end
@implementation ViewController
-(void)viewDidLoad
{
[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
-(void)processCompleted{
[myLabel setText:@"Operation Completed "];
}
@end

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

Check out our iOS Interview Questions to ace your next interview!

Course Schedule

Name Date Details
iOS Development Training 20 Apr 2024(Sat-Sun) Weekend Batch
View Details
iOS Development Training 27 Apr 2024(Sat-Sun) Weekend Batch
View Details
iOS Development Training 04 May 2024(Sat-Sun) Weekend Batch
View Details

Data-Science-Professional-Banner.jpg