cocoacocoa-sheet

How do I use the return value of a sheet to decide whether or not to close a window?


I want to use windowShouldClose: in my NSWindowController subclass to pop up a sheet asking if the user wants to save changes before closing with Save, Cancel, and Don't Save buttons.

The issue I'm running in to is that beginSheetModalForWindow:... uses a delegate instead of a return value.

I can return NO in windowShouldClose:, but then when I send [self close] to the controller in the panel's delegate nothing happens.

Can somebody explain to me how to do this or point me in the direction of some sample code?


Solution

  • The basic solution is to put a boolean flag on the window that states whether or not the window has warned about unsaved changes. Before calling [self close], set this flag to true.

    Finally, in the windowShouldClose method, return the value of the flag.