目标C术语:出口&代表们 [英] Objective C terminology: outlets & delegates

查看:182
本文介绍了目标C术语:出口&代表们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解iPhone的交易概念时遇到问题。帮帮我!代表也混淆了我。有人愿意解释吗?

I'm having issues understanding the concept of outlets how the iPhone deals with events. Help! Delegates confuse me too. Would someone care to explain, please?

推荐答案

Outlets (在Interface Builder中)是成员变量一个在运行时加载设计器时分配对象的类。 codeIBOutlet 宏(这是一个空的 #define )可以让Interface Builder将其识别为在设计师。

Outlets (in Interface Builder) are member variables in a class where objects in the designer are assigned when they are loaded at runtime. The IBOutlet macro (which is an empty #define) signals Interface Builder to recognise it as an outlet to show in the designer.

例如,如果我拖出一个按钮,然后将其连接到 aButton 我的界面.h文件),在运行时加载NIB文件将分配 aButton 指向 UIButton 实例化的指针由NIB。

For example, if I drag out a button, then connect it to the aButton outlet (defined in my interface .h file), the loading of the NIB file at runtime will assign aButton the pointer to that UIButton instantiated by the NIB.

@interface MyViewController : UIViewController {
    UIButton *aButton;
}

@property(nonatomic, retain) IBOutlet UIButton *aButton;

@end

然后执行:

@implementation MyViewController

@synthesize aButton; // Generate -aButton and -setAButton: messages

-(void)viewDidAppear {
    [aButton setText:@"Do Not Push. No, seriously!"];
}

@end

这样就无需写代码在运行时实例化和分配GUI对象。

This eliminates the need to write code to instantiate and assign the GUI objects at runtime.

对于代表,它们是另一个对象使用的事件接收对象(通常是通用的API类,如表视图)。他们没有什么特别的。这更像是一种设计模式。委托类可以定义几个预期的消息,例如:

As for Delegates, they are event receiving objects used by another object (usually a generalised API class such as a table view). There's nothing inherently special about them. It's more of a design pattern. The delegate class may define several of the expected messages such as:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

...并且API对象在需要时在委托上调用此消息通知事件。例如:

...and the API object calls this message on the delegate when it wants to notify it of the event. For example:

-(void)update:(double)time {
    if (completed) {
        [delegate process:self didComplete:totalTimeTaken];
    }
}

代理定义消息:

-(void)process:(Process *)process didComplete:(double)totalTimeTaken {
    NSString *log = [NSString stringWithFormat:@"Process completed in %2.2f seconds.", totalTimeTaken];
    NSLog(log);
}

这样使用可能是:

Process *proc = [Process new];
[proc setDelegate:taskLogger];
[proc performTask:someTask];

// Output:
// "Process completed in 21.23 seconds."

这篇关于目标C术语:出口&代表们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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