1

How do I override the close button action to show a confirm dialog? Would placing code in the app delegate's

applicationWillTerminate

be the correct place for such code?

I have an app using CloudKit. It saves way too constantly, where it causes errors trying to save to iCloud.

So I'm trying to show a dialog to confirm if the user wants to sync before quitting.

Naoto Ida
  • 1,275
  • 1
  • 14
  • 29
  • 1
    [Answer here](http://stackoverflow.com/questions/24088906/how-to-display-application-icon-in-menubar-even-application-is-quit/24094273#24094273) about `applicationShouldTerminate` may help, but you sound like you'd want to keep the window open, which would require a NSWindowController method, likely. – stevesliva Aug 12 '15 at 04:51

1 Answers1

3

If you want to block a window from closing, the NSWindow's delegate is where it's at. Be it either the AppDelegate (which is the default for most Xcode templates) or an NSWindowController if that's how you presented your window. The method you want to implement from NSWindowDelegate is -windowShouldClose: or -windowWillClose:. If you want to prevent the closing of the window, simply return NO from -windowShouldClose:.

If you want to stop the app from quitting, you should look into NSApplicationDelegate and override -applicationShouldTerminate: and return NSTerminateCancel to prevent the app from terminating.

In short, the "should" methods allow you to prevent the action from occurring, the "will" methods are just saying that it will happen and you can deal with it.

Lucas Derraugh
  • 6,929
  • 3
  • 27
  • 43