• Articles
  • Tutorials
  • Interview Questions

Delegate in 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.
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.

become an ios developer

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

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.

Course Schedule

Name Date Details
iOS Development Training 23 Nov 2024(Sat-Sun) Weekend Batch View Details
30 Nov 2024(Sat-Sun) Weekend Batch
07 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.