如何打开 NSWindow 并选择该窗口并使其处于焦点?
我正在尝试使用以下代码打开 NSWindow:
NSWindowController *window = [[NSWindowController alloc] initWithWindowNibName:@"MainWindow"];
[window showWindow:nil];
窗口打开正常,但前一个窗口仍然是 mainWindow 并且处于焦点。我尝试过以下代码来强制主窗口,但它不起作用。该窗口仍然有一个禁用的标题栏,并且不接受按键事件等。
[self.window makeKeyAndOrderFront:self];
[self.window makeMainWindow];
我似乎能够让前一个窗口失去焦点的唯一方法是,如果我在调用 showWindow: with [[NSApp mainWindow] close 后关闭窗口];
有什么想法吗?
I am trying to open a NSWindow using the following code:
NSWindowController *window = [[NSWindowController alloc] initWithWindowNibName:@"MainWindow"];
[window showWindow:nil];
The window opens okay but the previous window is still the mainWindow and in focus. I have tried the following code to force the main window and it doesn't work. The window still has a disabled title bar and isn't accepting key events etc.
[self.window makeKeyAndOrderFront:self];
[self.window makeMainWindow];
The only way I seem to be able to get the previous window to lose focus is if I close the window after calling showWindow: with [[NSApp mainWindow] close];
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
makeKeyAndOrderFront:
是正确的方法。您确定 self.window 和 window 引用同一个对象吗?makeKeyAndOrderFront:
is the way to go. Are you sure thatself.window
andwindow
refer the same object?我通过将 WindowController 分配给 nib 文件所有者解决了该问题,而不是在 nib 中拥有单独的 NSWindowController 对象。
I resolved the issue by assigning the WindowController to the nib File Owner, instead of having a separate NSWindowController object within the nib.