开发者

Make a NSAlert the topmost window?

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, i开发者_StackOverflow中文版t'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:

Make a NSAlert the topmost window?

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


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


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

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

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


If you want to support 10.5 also. you can use

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];


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* panel = static_cast<NSPanel*>([alert window]);
panel.floatingPanel = YES;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜