【发布时间】:2011-10-07 10:46:52
【问题描述】:
我目前正在尝试学习 Cocoa,但我不确定我是否理解正确...这是关于 delegates 和 controllers。
首先:这两者有什么区别?有时我看到一个类被称为 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 时,它都会向WindowController 发送-windowDidMiniaturize: 消息。我有这个权利吗?
如果是这样,你为什么不直接继承 NSWindow 而不是麻烦你必须处理的额外类?
【问题讨论】: