尝试将 NSWindow 设为 keyWindow 时的计时问题
我通过调用 which Works 来显示我的主窗口
[window makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
[window setIsVisible:YES];
[window display];
,但在调用此函数后不会立即将窗口设置为关键窗口。我必须等待“一段时间”,直到 [NSApp keyWindow]
返回实际的窗口
。
我现在想知道,这需要多长时间以及如何强制窗口立即成为关键窗口?
I show my main window by calling
[window makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
[window setIsVisible:YES];
[window display];
which works, but doesn't set the window to the key window right after this calls. I have to wait "some time" until [NSApp keyWindow]
returns the actual window
.
I'm wondering now, how long does this take and how can I force a window to become the key window immediately?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 makeKeyAndOrderFront 不是同步调用可能有充分的理由,即可能涉及 NSApplication 需要处理的多个窗口和对象的协调它发生了,所以 Cocoa 可能不支持强制窗口立即成为关键。然而,这可能不是问题,具体取决于您要解决的问题。
现在,我的猜测是,您的某些方法依赖于窗口作为关键,而目前它们没有正确发生,因为窗口不会立即成为
key
。但是,您可以实现NSWindowDelegate
协议,将自己设置为窗口委托,并重写- (void)windowDidBecomeKey:(NSNotification *)notification
方法来查明窗口何时执行成为关键。这也应该是一个全局通知,以防更适合您。有关更多详细信息,请查看 Apple 文档 http ://developer.apple.com/library/mac/#documentation/cocoa/reference/NSWindowDelegate_Protocol/Reference/Reference.html
I think there are probably good reasons that
makeKeyAndOrderFront
isn't a synchronous call, namely there could be coordination involved with multiple windows and objects thatNSApplication
need to take care of to make it happen, so forcing window to become key immediately is probably not supported by Cocoa. This however may not be a problem depending on the problem you are trying to solve.Now, my guess is that some of your methods depend on the window being key, and at the moment they are not happening properly because the window doesn't become
key
immediately. However, you can implement theNSWindowDelegate
protocol, set yourself as window delegate, and override- (void)windowDidBecomeKey:(NSNotification *)notification
method to find out when the window did become key. This should also be a global notification in case that works better for you.For more details, check out apple docs at http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSWindowDelegate_Protocol/Reference/Reference.html