【发布时间】:2012-01-11 15:51:18
【问题描述】:
我正在 XCode4 中编写一个 Cocoa/Objective-C 应用程序,我需要知道我的首选项面板何时打开。我需要像windowDidBecomeKey这样的回调;我尝试遵循this question 中提供的解决方案,但windowDidBecomeKey 或windowDidExpose 都没有作为委托方法出现(但其他方法,如windowDidLoad、windowWillLoad 等)。
为了澄清我所说的“不显示为委托方法”的确切含义,我的意思是当我开始输入方法名称时它们不会出现在自动完成中。我确实尝试过定义它们,但它们从未被调用过。
NSPanel 对象是否缺少这些方法,或者我还需要做些什么?
目前我有一个接口PrefWindowController:
PrefWindowController.h:
#import <Cocoa/Cocoa.h>
@interface PrefWindowController : NSWindowController
//Delegate methods like windowDidBecomeKey appear to not be available here
@end
PrefWindowController.m:
@implementation PrefWindowController
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:@".."];
[alert runModal];
}
return self;
}
- (void)windowDidLoad
{
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:@"Loaded"];
[alert runModal];
}
@end
当应用程序启动时从 .xib 加载窗口时,windowDidLoad 会触发并显示上面定义的通知。我这样做只是为了测试方法实际上被调用了。
关于如何在面板成为关键或获得焦点时获得回调的任何建议都会非常有帮助。
更新:
我向窗口控制器添加了一个windowDidBecomeKey 方法,如下所示:
PrefWindowController.h:
- (void)windowDidBecomeKey:(NSNotification *)notification;
PrefWindowController.m:
- (void)windowDidBecomeKey:(NSNotification *)notification
{
NSLog(@"Test");
}
我第一次打开窗口时会记录测试消息,但在我的main.m 文件的返回行上出现错误:
线程 1:程序接收信号:“EXC_BAD_ACCESS”
【问题讨论】:
标签: objective-c cocoa xcode4 nswindowcontroller nspanel