简体   繁体   English

从另一个NSAlert的didEndSelector调用NSAlert

[英]Calling an NSAlert from didEndSelector of another NSAlert

I need to bring up an NSAlert based on the response from another NSAlert. 我需要根据另一个NSAlert的回复提出一个NSAlert。 However, when I try to call it from the didEndSelector of the first one, all kinds of nasty things happen (like my document window disappearing and warnings about ordering problems printing to console). 但是,当我尝试从第一个的didEndSelector调用它时,会发生各种令人讨厌的事情(比如我的文档窗口消失,并且警告有关打印到控制台的排序问题)。

Any thoughts? 有什么想法吗?

What you're trying to do is "chain" the alerts. 你要做的是“链接”警报。

To do this you need to call orderOut: on the alert window. 为此,您需要在警报窗口中调用orderOut: .

Here's the documentation: 这是文档:

If you want to dismiss the sheet from within the alertDidEndSelector method before the modal delegate carries out an action in response to the return value, send orderOut: (NSWindow) to the window object obtained by sending window to the alert argument. 如果要在模态委托执行响应返回值的操作之前从alertDidEndSelector方法中解除工作表,请将orderOut :( NSWindow)发送到通过将窗口发送到alert参数获得的窗口对象。 This allows you to chain sheets, for example, by dismissing one sheet before showing the next from within the alertDidEndSelector method. 这允许您链接工作表,例如,通过在alertDidEndSelector方法中显示下一个工作表之前解除一个工作表。 Note that you should be careful not to call orderOut: on the sheet from elsewhere in your program before the alertDidEndSelector method is invoked. 请注意,在调用alertDidEndSelector方法之前,应该注意不要在程序中其他位置的工作表上调用orderOut :.

There is an easier way, simply check the contents of [runModal] in an if statement: 有一种更简单的方法,只需在if语句中检查[runModal]的内容:

//setup the dialog
NSAlert *networkErrorDialog = [NSAlert alertWithMessageText:@"Couldn't connect to the server" defaultButton:@"Network Diagnostics" alternateButton:@"Quit" otherButton:nil informativeTextWithFormat:@"Check that your computer is connected to the internet and make sure you aren't using a proxy server or parental controls"];

//show the dialog inside an IF, 0=the first button 1=the 2nd button etc
                if ([networkErrorDialog runModal]==0) {
                    //quit
                    [[NSApplication sharedApplication] terminate:self];
                } else {
                    //Network Diagnostics
                    [[NSWorkspace sharedWorkspace] launchApplication:@"Network Diagnostics"];
                    [[NSApplication sharedApplication] terminate:self];
                }

Hope that helps 希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM