【问题标题】:how delegates works and delegates work flow in objective-c委托如何工作和委托在objective-c中的工作流程
【发布时间】:2015-03-22 09:15:36
【问题描述】:

我是 Objective-c 的新手。我正在学习objective-c。您能否让我知道这段代码是如何工作的,以及您能否帮助理解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

在我在SampleProtocol.m下面的代码中添加之后

#import "SampleProtocol.h"

@implementation SampleProtocol

-(void)startSampleProcess{

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

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

#import <UIKit/UIKit.h>
#import "SampleProtocol.h"

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

更新的ViewController.m文件如下

#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

【问题讨论】:

    标签: ios objective-c delegates


    【解决方案1】:

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

    其次,您可以将&lt;SampleProtocolDelegate&gt; 移动到属性声明中,您还应该将其设置为weak 以避免保留循环。请参阅:Why use weak pointer for delegation?。所以你的界面最终会是这样的:

    @interface SampleProtocol : NSObject
    
    @property (nonatomic, weak) id <SampleProtocolDelegate> delegate;
    
    -(void)startSampleProcess;
    
    @end
    

    通过将&lt;SampleProtocolDelegate&gt; 放在“id”和“delegate”之间, 只有符合 SampleProtocolDelegate 的对象才能将自己设置为对象的委托(这意味着:任何符合此协议的对象)。 SampleProtocol 对象可以安全地假定它可以调用其委托的协议方法。

    【讨论】:

    • 感谢您向我解释清楚......但我不知道为什么人们不给我投票:(
    【解决方案2】:

    委托是开发人员库中的一个强大工具,我认为委托是一种连接对象并帮助它们与其他对象进行通信的干净而简单的方式。换句话说,delegation 是 Objective-C 对象的约会服务。假设我们有两个对象,Brain 和 Beer Bottle,Brain 是我们用来管理整个 Body 应用程序的对象,它处理所有重要的任务,例如便便,吃,喝,睡等。啤酒瓶附在身体上,但它不知道大脑在想什么,同样,大脑也不知道啤酒瓶在想什么。

    大脑在看电视时正在使用啤酒瓶的属性来满足自己,但问题是大脑被电视分散了注意力,以至于它无法注意啤酒何时用完。这一切都可能以灾难告终,Brain 需要知道什么时候啤酒是空的,以便将身体发送到冰箱并初始化啤酒瓶的另一个实例。

    Brain 可以使用drink 函数来降低Beer Bottles 液体变量,但是一旦液体达到0,Brain 需要知道它,这就是delegate 起作用的地方,我们可以使用Beer Bottle Delegate。 Brain 可以听到 Beer Bottle Delegate 告诉大脑瓶子是空的,我们需要做的只是告诉 Brain 倾听 Beer Bottle 告诉它的代表是空的,Brain 可以对此做出反应。这个经过深思熟虑和图解的图表显示了所有这些在行动中

    【讨论】:

    猜你喜欢
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多