Make a NSAlert the topmost window?

≡放荡痞女 提交于 2019-12-04 02:44:15

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!