Closing child windows in Cocoa when closing the main window

I'm a Cocoa newbie, so it's likely my approach is wrong, but ..

I have an application that opens multiple child windows (after loading the main / parent window) with NSWindowController

and initNibWIthName:

. This works great.

But when I close the parent window (using the red x) they stay open and prevent the application from closing until they are closed. This makes sense since I'm not shutting anyone down.

But how can I do this? There should be an event that is being called at this point, but I cannot find it anywhere.

Notifications like applicationWillTerminate

(etc.) are only called when the app actually exits not when the close button is pressed.

I am guessing that I am looking for something similar to WM_CLOSE

Windows type messages .

+2


source to share


3 answers


The closest equivalent you'll find is the NSWindowWillCloseNotification

one sent by the window before it closes. You can probably close child windows when the parent window is closed using:

NSWindow *parentWindow;
NSArray *childWindows;

NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
for (NSWindow *childWindow in childWindows) {
  [noteCenter
   addObserver:childWindow selector:@selector(close)
   name:NSWindowWillCloseNotification object:parentWindow];
}

      

If the child window gets deallocated before its parent, be sure to cancel it for notifications before this happens.



The delegate method mentioned by Mark is a convenience method for the delegate that saves them from having to register the notification that they probably want anyway. You don't need to create a window controller just to receive this message; just sending the window [window setDelegate:myObject]

will cause it to myObject

receive the message -windowWillClose:

if it responds to the method.

By the way, what Cocoa calls "child windows" is different from what you might think. They are not covered in the Window Programming Guide , but if you look at the documentation for the related methods on NSWindow

, you will find that they basically track the movements of their parent window, so they move with it.

If you are coming to Cocoa from Win32 programming, you can find Apple Porting to Mac OS X from Windows Win32 API to highlight the conceptual differences between Win32 and Cocoa.

+4


source


windowWillClose:



Apple Developer docs NSWindowDelegate

+3


source


In Mac OS X, Windows and applications are not the same.

If you have an interface with one window, with the main window and others, with the exception of "About", "Preferences", etc., you should implement applicationShouldTerminateAfterLastWindowClosed:

in the application's deletion and return YES

. This is the only way (other than doing it manually) that closing the window forces the application to exit.

If you have a multi-window interface (as in a typical document based application) then you have to do all these Windows peer mappings to each other. Windows like inspectors and tool palettes should be floating panels, not regular windows. And closing the last window should never stop after such an application.

+2


source







All Articles