close event NSWindow

Radu Paise picture Radu Paise · Feb 24, 2011 · Viewed 8.3k times · Source

I have an application, where a second NSWindow is opened by pressing a button. This new window is opened using [NSApp runModalForWindow:<myWindow>]. I want to be able to determine if the user closes the second window, in order to stop the modal.

Answer

Richard picture Richard · Feb 24, 2011

There are several ways to be notified when a window closes.

  • You can observe NSWindowWillCloseNotification notifications from the second NSWindow object.
  • You can implement NSWindowDelegate methods windowShouldClose: or windowWillClose:.
  • You can subclass NSWindow and override the performClose: method.
  • You can add a Close button to the window, and connect it to an action.

Without more information, it's hard to advise which of these or other options would work best for you.