Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

ios - how delegates works and delegates work flow in objective-c

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First 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 using self.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:

@interface SampleProtocol : NSObject

@property (nonatomic, weak) id <SampleProtocolDelegate> delegate;

-(void)startSampleProcess;

@end

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...