让NSAlert成为最顶层的窗口?

时间:2022-09-11 18:51:59

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).

另外,它是一个菜单栏应用程序(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:

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

让NSAlert成为最顶层的窗口?

See how the window isn't selected? Is there any way around this without changing my whole app window level? Thanks!

看看窗口没有被选中?有没有办法改变我的整个应用程序窗口级别?谢谢!

3 个解决方案

#1


9  

Have you tried activating your application in the code that displays the alert?

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

[[NSRunningApplication currentApplication] activateWithOptions:0];

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).

如果传递0不起作用,您可以将NSApplicationActivateIgnoringOtherApps作为选项传递,但除非确实有必要,Apple建议不要使用它(请参阅NSRunningApplication的文档)。


Update: You have activate before running the alert. This works for me in a new app with LSUIElement set:

更新:您在运行警报之前已激活。这对我来说适用于LSUIElement设置的新应用:

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

    [[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
    [alert runModal];
}

#2


2  

If you want to support 10.5 also. you can use

如果你想支持10.5也。您可以使用

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];

#3


0  

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':

强制应用程序移动到最前面是一个相当糟糕的主意。人们可能更喜欢使用专用的NSPanel属性“floatingPanel”使警报浮动在所有内容上:

NSPanel* panel = static_cast<NSPanel*>([alert window]);
panel.floatingPanel = YES;

#1


9  

Have you tried activating your application in the code that displays the alert?

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

[[NSRunningApplication currentApplication] activateWithOptions:0];

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).

如果传递0不起作用,您可以将NSApplicationActivateIgnoringOtherApps作为选项传递,但除非确实有必要,Apple建议不要使用它(请参阅NSRunningApplication的文档)。


Update: You have activate before running the alert. This works for me in a new app with LSUIElement set:

更新:您在运行警报之前已激活。这对我来说适用于LSUIElement设置的新应用:

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

    [[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
    [alert runModal];
}

#2


2  

If you want to support 10.5 also. you can use

如果你想支持10.5也。您可以使用

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];

#3


0  

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':

强制应用程序移动到最前面是一个相当糟糕的主意。人们可能更喜欢使用专用的NSPanel属性“floatingPanel”使警报浮动在所有内容上:

NSPanel* panel = static_cast<NSPanel*>([alert window]);
panel.floatingPanel = YES;