使NSAlert最顶层的窗口? [英] Make a NSAlert the topmost window?

查看:554
本文介绍了使NSAlert最顶层的窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  [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的文档)。






strong>更新:在运行警报之前已激活。这适用于我在一个新的应用程序与LSUIElement设置:

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

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


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

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:

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];
}

这篇关于使NSAlert最顶层的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆