将 NSAlert 设为最顶层窗口?
我在应用程序中创建了主窗口来进行以下设置:
[self setLevel:kCGDesktopWindowLevel + 1];
[self setCollectionBehavior:
(NSWindowCollectionBehaviorCanJoinAllSpaces |
NSWindowCollectionBehaviorStationary |
NSWindowCollectionBehaviorIgnoresCycle)];
这是一个非常自定义的窗口,漂浮在桌面上方。
此外,它还是一个菜单栏应用程序 (LSUIElement
)。
好的,所以如果出现问题,我需要显示警报。我是这样做的:
NSAlert *alert = [NSAlert alertWithMessageText:@""
defaultButton:@""
alternateButton:@""
otherButton:@""
informativeTextWithFormat:@""];
[alert runModal];
当然,我已经填写了按钮和其他文本。
这是我的问题:当我的应用程序当前不是关键应用程序时,弹出此警报,它不是关键窗口。像这样:
看看窗口是如何未被选中的?有没有办法解决这个问题而不改变我的整个应用程序窗口级别?谢谢!
I've created the main window in my application to have these settings:
[self setLevel:kCGDesktopWindowLevel + 1];
[self setCollectionBehavior:
(NSWindowCollectionBehaviorCanJoinAllSpaces |
NSWindowCollectionBehaviorStationary |
NSWindowCollectionBehaviorIgnoresCycle)];
It's a very custom window that sort of floats above the desktop.
In addition, it's a menu-bar application (LSUIElement
).
Alright, so I need to display an alert if something isn't right. Here's how I'm doing it:
NSAlert *alert = [NSAlert alertWithMessageText:@""
defaultButton:@""
alternateButton:@""
otherButton:@""
informativeTextWithFormat:@""];
[alert runModal];
Of course I have filled in the buttons and other text.
Here's my problem: When my application is not currently the key application, and this alert pops up, it's not a key window. Like this:
See how the window isn't selected? Is there any way around this without changing my whole app window level? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过在显示警报的代码中激活您的应用程序?
如果传递 0 不起作用,您可以传递 NSApplicationActivateIgnoringOtherApps 作为您的选项,但 Apple 建议不要这样做,除非确实有必要(请参阅 NSRunningApplication 的文档)。
更新:您在运行警报之前已激活。这在设置了 LSUIElement 的新应用程序中适用于我:
Have you tried activating your application in the code that displays the alert?
If passing 0 doesn't work, you can pass
NSApplicationActivateIgnoringOtherApps
as your option, but Apple recommends against it unless really necessary (see docs for NSRunningApplication).Update: You have activate before running the alert. This works for me in a new app with LSUIElement set:
如果你也想支持10.5。你可以使用
If you want to support 10.5 also. you can use
强制应用程序移动到最前面是一个相当糟糕的主意。
人们可能更喜欢使用专用的 NSPanel 属性“floatingPanel”使警报浮动在所有内容上:
Forcing an application to move forefront is a rather bad idea.
One would probably prefer to make the alert floating over everything using the dedicated NSPanel property 'floatingPanel':
如果您需要确保 NSAlert 位于最上面,请使用:
否则,如果您希望突出显示默认按钮,请使用(正如其他人所建议的那样):
If you need to ensure the NSAlert is topmost use:
Otherwise, if you want the default button to be highlighted, use (as others have suggested):