我在用着NSWindowController
从 NIB 加载窗口。然而,当我打电话时showWindow:
,该窗口在视觉上位于最上面,但焦点仍保持在原来的位置(而不是将其移动到新窗口)。
It's easy to see this happening when the first window (with keyboard focus) is moved slightly, before creating the new window (via cmd+n). This is the result:
![](https://i.stack.imgur.com/DumNO.png)
The bottom, focused window is the original window. The unfocused window on top is the newly created window.
这是相关代码:
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,未经修改。
主菜单.xib:
默认的 MainMenu.xib,其窗口被删除。
Calling makeKeyAndOrderFront:
在控制器的窗口上windowDidLoad
方法似乎没有聚焦到新窗口。将 FooWindow.xib 的文件所有者设置为FooController
似乎也没有帮助。
从 NIB 加载和显示窗口以使其接收键盘焦点的正确方法是什么?
Edit:看起来像NSWindowController
's window
方法返回nil
,这解释了为什么调用方法window
没有做任何事情。但为什么会这样nil
?