0
votes

If something goes wrong saving a file, I'd like to show the error alert as a sheet over the save sheet, as overwrite prompt does. However, the save panel closes immediately upon the completion of the completion block, taking the error alert with it.

[panel beginSheetModalForWindow:window
              completionHandler:^(NSInteger result) {
                  if (result == NSFileHandlingPanelOKButton) {
                      NSError *error;
                      // Do my saving here...
                      if (error)
                          [[NSAlert alertWithError:error] beginSheetModalForWindow:panel
                                                                     modalDelegate:nil
                                                                    didEndSelector:nil
                                                                       contextInfo:nil];
                  }
              }];

Can I cancel hiding the NSSavePanel from within the completion block? From a delegate? From anything?

1

1 Answers

2
votes

I just checked in TextEdit and what it does in the case you're after—not confirmation of the save, but failure of the save—is the following:

  1. The Save panel rolls up.
  2. The app tries and fails to save. (Your block.)
  3. The app presents its error sheet on the document window, with the Save panel long gone.

Screenshot of TextEdit's error sheet

So, if you want Apple-like behavior, show your alert sheet on the document window.

Incidentally, you may be interested in presentError:modalForWindow:delegate:didPresentSelector:contextInfo:.