让NSAlert成为最顶层的窗口?

时间:2011-03-11 04:57:53

标签: objective-c cocoa macos nsalert

我已在我的应用程序中创建了主窗口以进行这些设置:

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

这是一个非常自定义的窗口,可以在桌面上方浮动。

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

好的,所以如果出现问题,我需要显示警告。以下是我的表现:

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

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

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

enter image description here

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

3 个答案:

答案 0 :(得分:9)

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

[[NSRunningApplication currentApplication] activateWithOptions:0];

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


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

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

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

答案 1 :(得分:2)

如果你想支持10.5。你可以用

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];

答案 2 :(得分:0)

强迫应用程序走在最前面是一个坏主意。 人们可能更喜欢使用专用的NSPanel属性'floatingPanel'使警报悬浮在所有内容上:

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