代表们的工作流程和代表工作流程如何 [英] how delegates works and delegates work flow in objective-c

查看:181
本文介绍了代表们的工作流程和代表工作流程如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective-c的新手。我正在学习目标c。你会不会让我知道这段代码如何工作,以及你是否有意帮助理解代表在Objective-C中的工作流程

  SampleProtocol.h 
#import< Foundation / Foundation.h>

@protocol SampleProtocolDelegate< NSObject>
@required
- (void)processCompleted;
@end

@interface SampleProtocol:NSObject

{

id< SampleProtocolDelegate> _代表;

}
@property(nonatomic,strong)id delegate;

- (void)startSampleProcess;

@end

在我添加 SampleProtocol .m 以下代码

  #importSampleProtocol.h

@implementation SampleProtocol

- (void)startSampleProcess {

[NSTimer scheduledTimerWithTimeInterval:3.0 target:self.delegate
selector:@selector(processCompleted)userInfo:nil repeats :没有];
}
@end

创建一个 IBOutlet ,并将其命名为myLabel,并更新以下代码,以在 ViewController.h中采用SampleProtocolDelegate

  #import< UIKit / UIKit.h> 
#importSampleProtocol.h

@interface ViewController:UIViewController< SampleProtocolDelegate>
{
IBOutlet UILabel * myLabel;
}
@end

和更新后的 ViewController .m 文件如下

  #importViewController.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 - 示例协议委托
- (void)processCompleted {
[myLabel setText:@Process Completed];
}


@end


解决方案

首先让我指出,当您使用 @property 声明时,不需要使用下划线前缀创建单独的实例变量一个财产您可以使用 self.delegate 访问此属性,它还会为您自动创建 _delegate 。因为 _delegate 已经使用 @property 创建,您可以取出重复的声明。



其次,您可以将< SampleProtocolDelegate> 移动到属性声明中,您还应将其设置为弱至避免保留周期。请参阅:为什么使用弱指针进行委派?所以你的界面最终会如下所示:

  @interface SampleProtocol:NSObject 

@property nonatomic,weak)id< SampleProtocolDelegate>代表;

- (void)startSampleProcess;

@end

< SampleProtocolDelegate> ; 在'id'和'delegate'之间,
只有符合SampleProtocolDelegate的对象可以将自己设置为对象的委托(这意味着:符合此协议的任何对象)。而SampleProtocol对象可以安全地假定它可以调用其委托的协议方法。


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

解决方案

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.

这篇关于代表们的工作流程和代表工作流程如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆