i am novice in Objective-c . i am learning objective-c . would you kindly let me know how this code work as well as would you kindly help to understand delegates work flow in objective-c
SampleProtocol.h
#import <Foundation/Foundation.h>
@protocol SampleProtocolDelegate <NSObject>
@required
- (void) processCompleted;
@end
@interface SampleProtocol : NSObject
{
id <SampleProtocolDelegate> _delegate;
}
@property (nonatomic,strong) id delegate;
-(void)startSampleProcess;
@end
after i added in SampleProtocol.m
below code
#import "SampleProtocol.h"
@implementation SampleProtocol
-(void)startSampleProcess{
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self.delegate
selector:@selector(processCompleted) userInfo:nil repeats:NO];
}
@end
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;
}
@end
and The Updated ViewController.m
file is as follows
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
SampleProtocol *sampleProtocol = [[SampleProtocol alloc]init];
sampleProtocol.delegate = self;
[myLabel setText:@"Processing..."];
[sampleProtocol startSampleProcess];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Sample protocol delegate
-(void)processCompleted{
[myLabel setText:@"Process Completed"];
}
@end
Delegation is a powerful tool in a developers arsenal, i see delegation as a clean and simple way of connecting objects and helping them to communicate with other. In other words, delegation is a dating service for Objective-C objects.Let's say we have two objects, Brain and Beer Bottle, Brain is the object we use to manage the entire Body application, it handles all of the important tasks such as poop, eat, drink, sleep etc. Beer Bottle is attached to body but it doesn't know what Brain is thinking, likewise, Brain has no idea what Beer Bottle is thinking.
Brain is using Beer Bottle's attributes to satisfy itself while it's watching TV, but the problem is that brain is so distracted by the tv that it can't pay attention to when beer is going to run out. This could all end in disaster, Brain needs to know when beer is empty so that it send body to the fridge and initialize another instance of Beer Bottle.
Brain can use the drink function to lower Beer Bottles liquid variable, but once liquid reaches 0, Brain needs to know about it, this is where delegates come into action, we can use a
Beer Bottle Delegate
. Brain can listen out for the Beer Bottle Delegate telling brain that the bottle is empty, all we need to do is simply tell Brain to listen out for Beer Bottle telling it's delegate that is empty and Brain can react to it. This well thought out and illustrated diagram shows all of this in actionFirst of all let me point out that you do not need to create a separate instance variable with an underscore prefix when you use
@property
to declare a property. You can access this property usingself.delegate
and it also automatically creates_delegate
for you. Because_delegate
is already created using@property
you can take out the duplicate declaration.Secondly, you can move
<SampleProtocolDelegate>
to the property declaration, you should also set it to weak to avoid a retain cycle. See: Why use weak pointer for delegation?. So your interface would end up looking like this:By putting
<SampleProtocolDelegate>
between 'id' and 'delegate', only objects that conform to the SampleProtocolDelegate can set themselves as the delegate of the object (it means: any object that conforms to this protocol). And the SampleProtocol object can safely assume that it can call the protocol methods on its delegate.