我有一个Cocoa应用程序,它使用NSAlert该类显示应用程序模式警报.我希望警报窗口浮动在所有其他应用程序的窗口之上.可以这样做NSAlert,还是我需要实现自己的窗口?
我不知道这是否重要,但应用程序是一个代理应用程序(LSUIElement是真的)实现为NSStatusItem.(有关该应用程序的更多信息,包括源代码,请查看<here>.)
以下是显示警报的代码:
- (void)showTimerExpiredAlert {
[NSApp activateIgnoringOtherApps:YES];
NSAlert *alert = [[NSAlert alloc] init];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert setMessageText:NSLocalizedString(@"Menubar Countdown Complete", @"Expiration message")];
[alert setInformativeText:NSLocalizedString(@"The countdown timer has reached 00:00:00.",
@"Expiration information")];
[alert addButtonWithTitle:NSLocalizedString(@"OK", @"OK button title")];
[alert addButtonWithTitle:NSLocalizedString(@"Restart Countdown...", @"Restart button title")];
NSInteger clickedButton = [alert runModal];
[alert release];
if (clickedButton == NSAlertSecondButtonReturn) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
我在runModal电话会议前试过这个:
[[alert window] setFloatingPanel:YES];
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
[[alert window] setLevel:NSFloatingWindowLevel];
Run Code Online (Sandbox Code Playgroud)
但是如果我点击另一个应用程序的窗口,这些都不会使窗口保持在其他窗口之上.我怀疑runModal只是不尊重这些设置.
我不久前破坏了我的大脑.
我可以让它工作(唯一的方法)的唯一方法是继承NSApplication,并覆盖-sendEvent.在-sendEvent中,你首先调用super的实现,然后执行以下操作:
id *modalWindow = [self modalWindow];
if (modalWindow && [modalWindow level] != MY_DESIRED_MODAL_WINDOW_LEVEL)
[modalWindow setLevel: MY_DESIRED_MODAL_WINDOW_LEVEL];
Run Code Online (Sandbox Code Playgroud)
除此之外,即使这样做也没有一丝不苟 - 当切换应用程序时 - 你永远不会想要这样做,因为它是一个公然的粗暴黑客.
所以,是的,遗憾的是你最好自己编写自己的NSAlert版本.如果你真的关心这种可能性,我会在上面提出一个错误.非常奇怪的是,[[alert window] setLevel:someLevel]不受NSApplication的尊重,而且为了能够做到这一点,必须重新构建具有所有整洁的小自动布局功能的NSAlert是一种浪费.