gpt4 book ai didi

objective-c - 让 NSAlert 成为最顶层的窗口?

转载 作者:太空狗 更新时间:2023-10-30 03:28:46 25 4
gpt4 key购买 nike

我已经在我的应用程序中创建了具有这些设置的主窗口:

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

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

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

好的,所以我需要在出现问题时显示警报。这是我的做法:

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

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

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

enter image description here

查看未选中窗口的原因?有没有办法在不改变我的整个应用程序窗口级别的情况下解决这个问题?谢谢!

最佳答案

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

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

关于objective-c - 让 NSAlert 成为最顶层的窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5269116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com