我正在使用NSWindowController
从 NIB 加载窗口。但是,当我调用 时showWindow:
,该窗口在视觉上位于最顶层,但焦点仍保留在原处(而不是将其移动到新窗口)。
在创建新窗口(通过 cmd+n)之前稍微移动第一个窗口(带有键盘焦点)时,很容易看到这种情况发生。结果如下:
底部的焦点窗口是原始窗口。顶部未聚焦的窗口是新创建的窗口。
这是相关代码:
AppDelegate.h:
- (IBAction)newDocument:(id) sender;
AppDelegate.m:
- (IBAction)newDocument:(id) sender {
[[[FooController alloc] init] showWindow:self];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self newDocument:self];
}
FooController.h:
@interface FooController : NSWindowController { }
@end
FooController.m:
- (id)init {
self = [super initWithWindowNibName:@"FooWindow"];
return self;
}
FooWindow.xib:
一个新创建的 Window xib,没有修改。
MainMenu.xib:
默认的 MainMenu.xib,其窗口已删除。
makeKeyAndOrderFront:
在控制器的方法中调用窗口windowDidLoad
似乎不会聚焦新窗口。将文件的 FooWindow.xib 所有者设置为FooController
也似乎没有帮助。
从 NIB 加载和显示窗口以使其接收键盘焦点的正确方法是什么?
编辑:它看起来像NSWindowController
's window
method returns nil
,这解释了为什么调用方法window
不做任何事情。但这是为什么呢nil
?