我正在尝试学习Cocoa,我不确定我是否理解正确...这是关于代表和控制器.
起初:两者有什么区别?有时我会看到调用类的代码AppController,有时候 - 或多或少相同的内容 - AppDelegate.
因此,如果我理解正确,委托就是一个简单的对象,它在某个事件发生时接收消息.例如:
@interface WindowController : NSObject <NSWindowDelegate>
@end
@implementation WindowController
- (void)windowDidMiniaturize:(NSNotification *)notification {
NSLog(@"-windowDidMiniaturize");
}
@end
Run Code Online (Sandbox Code Playgroud)
现在,我使用此代码使其成为我的代表window:
@interface TryingAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
}
@property (assign) IBOutlet NSWindow *window;
@property (retain) WindowController *winController;
@end
Run Code Online (Sandbox Code Playgroud)
通过以下实现:
@implementation TryingAppDelegate
@synthesize window;
@synthesize winController;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"-applicationDidFinishLaunching:");
self.winController = [[WindowController alloc] init];
[window setDelegate:winController];
[self.winController release];
}
@end
Run Code Online (Sandbox Code Playgroud)
现在,每当我最小化时window,它都会向我发送-windowDidMiniaturize:消息WindowController …