如何将重点放在从NIB加载的NSWindow? [英] How to give focus to NSWindow loaded from NIB?
问题描述
我使用 NSWindowController
从NIB加载窗口。但是,当我调用 showWindow:
时,窗口在视觉上最顶端,但焦点仍然在它的位置(而不是移动到新窗口)。
在创建新窗口之前(通过cmd + n),第一个窗口(使用键盘焦点)稍微移动很容易看到这种情况。这是结果:
底部的焦点窗口是原窗口。顶部的未聚焦窗口是新创建的窗口。
这是相关代码:
AppDelegate.h :
- (IBAction)newDocument:
AppDelegate.m:
- (IBAction)newDocument:(id)sender {
[[[FooController alloc] init] showWindow:self];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self newDocument:self];
}
FooController.h:
@interface FooController:NSWindowController {}
@end
$ b b
FooController.m:
- (id)init {
self = [super initWithWindowNibName:@ FooWindow];
return self;
}
FooWindow.xib:
新创建的Window xib
MainMenu.xib:
默认的MainMenu.xib,其窗口已删除。
在控制器的 windowDidLoad
方法的窗口中调用 makeKeyAndOrderFront:
窗口。将FooWindow.xib的文件所有者设置为 FooController
也没有帮助。
什么是正确的方式要加载并显示来自NIB的窗口,以便它能接收键盘焦点?
编辑:看起来像 NSWindowController 的
不做任何事情。但为什么窗口
方法返回 nil
,这解释了为什么调用< $ c> window nil
?
xib文件的所有者必须设置为控制器,并且(这是我不知道的部分)你必须连接控制器的 window
插入窗口本身。
完成后,它只是工作。不需要 makeKeyWindow
, makeMainWindow
或 makeKeyAndOrderFront:
p>
I'm using NSWindowController
to load a window from a NIB. However, when I call showWindow:
, the window is visually topmost, but the focus remains where it was (instead of moving it to the new window).
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:
The bottom, focused window is the original window. The unfocused window on top is the newly created window.
This is the relevant code:
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:
A freshly created Window xib, without modifications.
MainMenu.xib:
The default MainMenu.xib, with its window deleted.
Calling makeKeyAndOrderFront:
on the window in the controller's windowDidLoad
method does not appear to focus the new window. Setting the File's owner of FooWindow.xib to FooController
also did not appear to help.
What is the correct way to load and show a window from a NIB so that it does receive keyboard focus?
Edit: It looks like NSWindowController
's window
method returns nil
, which explains why calling methods on window
doesn't do anything. But why is it nil
?
Okay, I found the cause of this problem.
The xib's File's owner must be set to the controller, and (this is the part I didn't know about) you have to connect the controller's window
outlet to the window itself.
Having done that, it just works. No makeKeyWindow
, makeMainWindow
or makeKeyAndOrderFront:
needed.
这篇关于如何将重点放在从NIB加载的NSWindow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!