So, here's my case :
I'm developing a NON-standard document-based application and I want to handle the scenario of aborting the termination when there still are unsaved changes in any of the documents.
My initial idea is :
- User tries to quit the application (either through the
"Quit XXXXX"
menu item or by clicking the"X"
button) - Are there any modified documents?
- if NO : quit
- if YES : notify user that there are unmodified documents. if he wants to proceed, then quit. Else cancel termination.
So, I decided to do it the delegate-way.
In my Application delegate, I've implemented :
-windowToClose:
(triggered onNSWindowWillCloseNotification
)applicationShouldTerminate:
applicationShouldTerminateAfterLastWindowClosed:
Now, that's what I'm thinking. In the applicationShouldTerminate:
method :
- Return
NSTerminateNow
if no modified documents exist - Return
NSTerminateCancel
if there ARE modified documents. In that case, pop up an alert sheet or something like that and if user confirms he wants to quit, then call a[NSApp terminate:nil];
Is that the right way to go about it?
Any suggestions?
That's indeed how to do it. But in your
applicationShouldTerminate
implementation, you should just pop up the alert right there, and returnNSTerminateNow
if the user didn't click Cancel;NSTerminateCancel
otherwise. No need to manually terminate the application later.Edit: to show an alert as a sheet but run it as a modal dialog, call
beginSheetModalForWindow
on the alert, then call[NSApp runModalForWindow:alert]
. In addition, in the end selector that you pass forbeginSheetModalForWindow
, you need to call[NSApp stopModal]
to get out of the modal loop.However, it is better in this case to run the alert as a sheet for the window, and return
NSTerminateLater
in yourapplicationShouldTerminate
implementation. Then, in the end selector for the alert, call[NSApp replyToApplicationShouldTerminate:]
. This is how Apple recommends you implement this.