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

什么是Cocoa中的委托,我为什么要使用它们?,cocoa,delegates,Cocoa,Delegates,我目前正在尝试学习可可,我不确定我是否理解正确。。。它是关于代理和控制器 首先:这两者有什么区别?有时我看到一个类被称为AppController,有时-内容或多或少相同-AppDelegate 所以,如果我理解正确的话,委托是一个简单的对象,它在某个事件发生时接收消息。例如: @interface WindowController : NSObject <NSWindowDelegate> @end @implementation WindowController - (void

我目前正在尝试学习可可,我不确定我是否理解正确。。。它是关于代理控制器

首先:这两者有什么区别?有时我看到一个类被称为
AppController
,有时-内容或多或少相同-
AppDelegate

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

@interface WindowController : NSObject <NSWindowDelegate>
@end

@implementation WindowController
- (void)windowDidMiniaturize:(NSNotification *)notification {
    NSLog(@"-windowDidMiniaturize");
}
@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
现在,每当我最小化
窗口
,它都会向
窗口控制器
发送一条
-windowdidmimize:
消息。我有这个权利吗


如果是这样的话,为什么不直接创建一个子类
NSWindow
,而不是处理一个额外的类呢?

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

一个类可以采用任意数量的协议,因此
是完全有效的。然后,可以将对象设置为任意数量窗口和文本字段的代理。要了解类如
NSTextField
支持哪些委托消息,请查看。
-delegate
-setDelegate:
方法通常会将您指向正确的协议。在我们的例子中,这是一个很好的例子。对于添加到旧版本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
@接口MyWindowController:NSWindowController{
NSTextField*_someTextField;
}
@属性(非原子,保留)ibnstextfield*someTextField;
@结束
@MyWindowController的实现
@合成someTextField=\u someTextField;
-(无效)解除锁定{
[_sometextfieldrelease];
[super dealoc];
}
-(无效)窗口加载{
[超级窗口加载];
//使用XIB/NIB时,还可以将文件的所有者设置为
//窗口和文本字段的委托。
[[self window]setDelegate:self];
[[self someTextField]setDelegate:self];
}
-(无效)WindowDid小型化:(NSNotification*)通知{
}
-(BOOL)控件:(NSControl*)控件文本应编辑:(NSText*)字段编辑器{
返回YES;
}
@结束

AppController
AppDelegate
只是同一类的不同命名约定。

谢谢!我的控制器需要符合什么协议?我想当我使用NSWindowDelegateProtocol时,我不能使它成为
NSTextField
的委托,是吗?我从这些元素接收到哪些消息?仍然
WindowDidMinimize
等等?我已经更新了我的答案,包括一些关于(代理)协议的信息和一个示例。
@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