在Cocoa中设置NSAlert的级别 [英] Set level for NSAlert in Cocoa
问题描述
在我的应用程序中,我对NSWindow进行了子类化,并将窗口级别设置为25.由于窗口级别为25,窗口会隐藏警告框和错误对话框。
有任何机会设置NSAlert的级别
首先。你不应该使用像25这样的幻数。
有一种设置窗口级别的方法,但它是无用的,因为runModal使用固定的windowLevel常数kCGModalPanelWindowLevel,它是8.你可以验证它像这样:
[self.window setLevel:25];
NSAlert * alert = [NSAlert alertWithMessageText:@1defaultButton:@2alternateButton:nil otherButton:nil informativeTextWithFormat:@3];
[alert runModal];
(lldb)po [alert.window valueForKey:@level]
8
#define NSModalPanelWindowLevel kCGModalPanelWindowLevel
解决方案:
-
p>
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse response){
}];
- 重新创建NSAlert功能作为NSWindow / NSPanel的子类(不继承NSAlert),并调用showWindow:如果你需要显示它。
In my application I've subclassed the NSWindow and set the window level as 25. Since the window level is 25 the alert box and error dialog box were being hidden by the window.
Is there any chance to set the level of NSAlert
First of all. You shouldn't use magic numbers like 25.
There is a way to set window level but it's useless because runModal uses fixed windowLevel constant kCGModalPanelWindowLevel which is 8. You can verify it like this:
[self.window setLevel:25];
NSAlert *alert = [NSAlert alertWithMessageText:@"1" defaultButton:@"2" alternateButton:nil otherButton:nil informativeTextWithFormat:@"3"];
[alert runModal];
(lldb) po [alert.window valueForKey:@"level"]
8
#define NSModalPanelWindowLevel kCGModalPanelWindowLevel
Solution:
Use sheet
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse response){
}];
Swizzle implementation runModal with your own one.
Recreate NSAlert functionality as a subclass of NSWindow/NSPanel (don't inherit NSAlert) and call showWindow: if you need to display it.
这篇关于在Cocoa中设置NSAlert的级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!