Calling NSAlert from didEndSelector of another NSAlert

I need to raise NSAlert based on another NSAlert's answer. However, when I try to call it from the didEndSelector first, all sorts of nasty things happen (like my document disappearing and warnings on how to print console issues).

Any thoughts?

+2


source to share


2 answers


What you are trying to do is "chain" warnings.

For this you need to call orderOut:

in the alert box.



Here's the documentation:

If you want to remove the sheet from in the alertDidEndSelector method before executing the modal delegate action in response to a return value, send orderOut: (NSWindow) to the window object obtained by sending the window to the alert argument. This allows you to swap sheets, for example, rejecting one sheet before showing the next from within the alertDidEndSelector method. Note that you must be careful not to call orderOut: on the sheet from elsewhere in your program before the alertDidEndSelector method is called.

+5


source


There is an easier way, just check the content [runModal]

in the if statement:

//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 it helps

+4


source







All Articles