将 NSAlert 设为最顶层窗口?

发布于 2024-10-21 12:58:58 字数 896 浏览 1 评论 0原文

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

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

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

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

好的,所以如果出现问题,我需要显示警报。我是这样做的:

NSAlert *alert = [NSAlert alertWithMessageText:@"" 
                                 defaultButton:@"" 
                               alternateButton:@"" 
                                   otherButton:@"" 
                     informativeTextWithFormat:@""];
[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:

enter image description here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不及他 2024-10-28 12:58:58

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

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

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];
}
无语# 2024-10-28 12:58:58

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

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];

If you want to support 10.5 also. you can use

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
南渊 2024-10-28 12:58:58

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

NSPanel* panel = static_cast<NSPanel*>([alert window]);
panel.floatingPanel = 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;
猫九 2024-10-28 12:58:58

如果您需要确保 NSAlert 位于最上面,请使用:

alert.window.level = NSFloatingWindowLevel;

否则,如果您希望突出显示默认按钮,请使用(正如其他人所建议的那样):

[[NSApplication sharedApplication] activateIgnoringOtherApps : YES];

If you need to ensure the NSAlert is topmost use:

alert.window.level = NSFloatingWindowLevel;

Otherwise, if you want the default button to be highlighted, use (as others have suggested):

[[NSApplication sharedApplication] activateIgnoringOtherApps : YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文