将 NSAlert 设为最顶层窗口?

2024-06-23

我在应用程序中创建了主窗口以进行以下设置:

[self setLevel:kCGDesktopWindowLevel + 1];
[self setCollectionBehavior:
     (NSWindowCollectionBehaviorCanJoinAllSpaces | 
      NSWindowCollectionBehaviorStationary | 
      NSWindowCollectionBehaviorIgnoresCycle)];

这是一个非常自定义的窗口,有点漂浮在桌面上方。

此外,它是一个菜单栏应用程序(LSUIElement).

好的,所以如果出现问题,我需要显示警报。我是这样做的:

NSAlert *alert = [NSAlert alertWithMessageText:@"" 
                                 defaultButton:@"" 
                               alternateButton:@"" 
                                   otherButton:@"" 
                     informativeTextWithFormat:@""];
[alert runModal];

当然,我已经填写了按钮和其他文本。

这是我的问题:当我的应用程序当前不是关键应用程序时,弹出此警报,它不是关键窗口。像这样:

看看窗口是如何未被选中的?有没有办法解决这个问题而不改变我的整个应用程序窗口级别?谢谢!


您是否尝试过在显示警报的代码中激活您的应用程序?

[[NSRunningApplication currentApplication] activateWithOptions:0];

如果传0不行的话可以传NSApplicationActivateIgnoringOtherApps作为您的选择,但苹果建议不要这样做,除非确实有必要(请参阅 NSRunningApplication 的文档)。


Update:您在运行警报之前已激活。这在设置了 LSUIElement 的新应用程序中适用于我:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSAlert *alert = [NSAlert alertWithMessageText: @"Blah"
                                     defaultButton: @"Blah"
                                   alternateButton: @"Blah"
                                       otherButton: @"Blah"
                         informativeTextWithFormat: @"Blah"];

    [[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
    [alert runModal];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 NSAlert 设为最顶层窗口? 的相关文章

随机推荐