Cocoa 中的委托是什么?我为什么要使用它们?

发布于 2024-12-08 19:00:18 字数 1331 浏览 0 评论 0原文

我目前正在尝试学习 Cocoa,我不确定我是否理解正确......这是关于代表控制器

首先:这两者有什么区别?有时我会看到代码中的类名为 AppController,有时 - 内容或多或少相同 - AppDelegate

因此,如果我理解正确的话,委托是一个简单的对象,它在发生特定事件时接收消息。例如:

@interface WindowController : NSObject <NSWindowDelegate>
@end

@implementation WindowController
- (void)windowDidMiniaturize:(NSNotification *)notification {
    NSLog(@"-windowDidMiniaturize");
}
@end

现在,我使用此代码使其成为我的 window 的委托:

@interface TryingAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
}

@property (assign) IBOutlet NSWindow *window;
@property (retain) WindowController *winController;

@end

通过以下实现:

@implementation TryingAppDelegate

@synthesize window;
@synthesize winController;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSLog(@"-applicationDidFinishLaunching:");

    self.winController = [[WindowController alloc] init];
    [window setDelegate:winController];

    [self.winController release];
}

@end

现在,每当我最小化 window 时,它都会发送 < code>-windowDidMiniaturize: 消息发送至 WindowController。我有这个权利吗?

如果是这样,为什么你不直接子类化 NSWindow 而不是费心去处理一个你必须处理的额外类呢?

I'm currently trying learning Cocoa and I am not sure if I understand that correctly... It's about delegates and controllers.

At first: What's the difference between the two? Sometimes I see code where a class is called AppController, sometimes - with more or less the same content - AppDelegate.

So, if I understand it correctly, a delegate is a simple object that receives messages when a certain event occurs. For example:

@interface WindowController : NSObject <NSWindowDelegate>
@end

@implementation WindowController
- (void)windowDidMiniaturize:(NSNotification *)notification {
    NSLog(@"-windowDidMiniaturize");
}
@end

Now, I use this code to make it a delegate of my window:

@interface TryingAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
}

@property (assign) IBOutlet NSWindow *window;
@property (retain) WindowController *winController;

@end

With the following implementation:

@implementation TryingAppDelegate

@synthesize window;
@synthesize winController;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSLog(@"-applicationDidFinishLaunching:");

    self.winController = [[WindowController alloc] init];
    [window setDelegate:winController];

    [self.winController release];
}

@end

And now, whenever I minimize window, it will send a -windowDidMiniaturize: message to WindowController. Do I have that right?

If so, why don't you just subclass NSWindow rather than bothering with an additional class you have to take care of?

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

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

发布评论

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

评论(1

ま昔日黯然 2024-12-15 19:00:18

当您希望一个对象协调多个其他对象时,委托尤其有用。例如,您可以创建一个 NSWindowController 子类并使其成为窗口的委托。同一个窗口可能包含您想要使窗口控制器成为其委托的几个其他元素(例如 NSTextField)。这样您就不需要对窗口及其几个控件进行子类化。您可以将概念上属于同一类的所有代码保留在一起。此外,委托通常属于模型-视图-控制器概念的控制器级别。通过子类化NSWindow,您可以将控制器类型代码移动到视图级别。

一个类可以采用任意数量的协议,因此 是完全有效的。然后,您可以将对象设置为任意数量的窗口和文本字段的委托。要了解像 NSTextField 这样的类支持哪些委托消息,请查看 类引用-delegate-setDelegate: 方法通常会将您指向正确的协议。在我们的例子中,这是 <代码>NSTextFieldDelegate。对于已添加到旧版本 Apple 框架中的类,通常会有一个关于委托方法的附加部分(或者与“类方法”和“实例方法”一起,或者作为“任务”的一个小节)。请注意,将您的类声明为符合委托协议不会让它们神奇地传递给您的对象 - 您必须显式将其设置为委托:

@interface MyWindowController : NSWindowController <NSWindowDelegate, NSTextFieldDelegate> {
    NSTextField *_someTextField;
}

@property (nonatomic, retain) IBOutlet NSTextField *someTextField;

@end


@implementation MyWindowController

@synthesize someTextField = _someTextField;

- (void)dealloc {
    [_someTextField release];
    [super dealloc];
}

- (void)windowDidLoad {
    [super windowDidLoad];
    // When using a XIB/NIB, you can also set the File's Owner as the
    // delegate of the window and the text field.
    [[self window] setDelegate:self];
    [[self someTextField] setDelegate:self];
}

- (void)windowDidMiniaturize:(NSNotification *)notification {

}

- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor {
    return YES;
}

@end

AppControllerAppDelegate 只是不同相同类型的类的命名约定。

Delegates are especially useful when you want one object to coordinate several others. For instance, you might create an NSWindowController subclass and make it it's window's delegate. That same window might contain several other elements (like NSTextFields) that you want to make the window controller a delegate of. This way you don't need to subclass the window and several of its controls. You can keep all the code that conceptually belongs together in the same class. In addition, delegates usually belong to the controller level of the Model-View-Controller concept. By subclassing NSWindow you would move controller type code to the view level.

A class can adopt any number of protocols, so <NSWindowDelegate, NSTextFieldDelegate> is perfectly valid. You can then set your object as the delegate of any number of windows and text fields. To find out what delegate messages a class like NSTextField supports check out the class reference.The -delegate and -setDelegate: methods will usually point you to the proper protocol. In our case this is NSTextFieldDelegate. For classes that have been added to older version of Apple’s frameworks there often an additional section on delegate methods (either alongside “Class Methods” and “Instance Methods” or as a subsection of “Tasks”). Note that declaring your class as conforming to a delegate protocol will not have them magically delivered to your object – you must explicitly set it as the delegate:

@interface MyWindowController : NSWindowController <NSWindowDelegate, NSTextFieldDelegate> {
    NSTextField *_someTextField;
}

@property (nonatomic, retain) IBOutlet NSTextField *someTextField;

@end


@implementation MyWindowController

@synthesize someTextField = _someTextField;

- (void)dealloc {
    [_someTextField release];
    [super dealloc];
}

- (void)windowDidLoad {
    [super windowDidLoad];
    // When using a XIB/NIB, you can also set the File's Owner as the
    // delegate of the window and the text field.
    [[self window] setDelegate:self];
    [[self someTextField] setDelegate:self];
}

- (void)windowDidMiniaturize:(NSNotification *)notification {

}

- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor {
    return YES;
}

@end

AppController and AppDelegate are just different naming conventions for the same type of class.

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