尝试将 NSWindow 设为 keyWindow 时的计时问题

发布于 2024-12-26 00:37:53 字数 319 浏览 0 评论 0原文

我通过调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦冥 2025-01-02 00:37:53

我认为 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 that NSApplication 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 the NSWindowDelegate 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文