views:

249

answers:

2

I need to bring up an NSAlert based on the response from another 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).

Any thoughts?

+4  A: 

What you're trying to do is "chain" the alerts.

To do this you need to call orderOut: on the alert window.

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. This allows you to chain sheets, for example, by dismissing one sheet before showing the next from within the alertDidEndSelector method. Note that you should be careful not to call orderOut: on the sheet from elsewhere in your program before the alertDidEndSelector method is invoked.

Leibowitzn
A: 

There is an easier way, simply check the contents of [runModal] in an 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 that helps

Cal S