Objective C 术语:outlet &代表们

发布于 2024-08-17 21:37:08 字数 56 浏览 5 评论 0原文

我无法理解 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨光如昨 2024-08-24 21:37:08

Outlet(在 Interface Builder 中)是类中的成员变量,设计器中的对象在运行时加载时会被分配。 IBOutlet 宏(这是一个空的 #define)向 Interface Builder 发出信号,将其识别为要在设计器中显示的插座。

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

@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 对象的需要。


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

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

...并且当 API 对象想要向委托通知事件时,它会在委托上调用此消息。例如:

-(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."

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.

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

Then in the implementation:

@implementation MyViewController

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

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

@end

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


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

...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];
    }
}

And the delegate defines the message:

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

Such use might be:

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

// Output:
// "Process completed in 21.23 seconds."
从来不烧饼 2024-08-24 21:37:08

委托是另一个对象可以向其转发消息的对象。换句话说,就像你妈妈叫你打扫房间,你却把它抵押给了你的弟弟。你的弟弟知道如何做这项工作(因为你懒得学),所以他会为你做这件事。

A delegate is a object that another object can forward messages to. In other words, it's like when your mom told you to clean your room and you pawned it off on your little brother. Your little bro knows how to do the job (since you were too lazy to ever learn) and so he does it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文