1
votes

many thanks for this site. many posts have helped me recently.

this window question seems simple, but I cannot find a simple reference to resolve my need. I'm building a simple app that manages printers. The main screen has a tableview that is driven by an array of data pulled from the cups server. Basically it shows the installed printers on that computer. There are two sub windows. Each with its own window controller and nib file. For clarity I'll call them sub nibs or sub window controllers. Each one does something a bit different, but affects the system printers. I want to make sure that when the sub window closes and returns control back to the main screen, the main screen window (being controlled by the AppDelegate) responds by reloading the tableview (to update any changes at the OS level with printers).

At a high level, I don't understand how to send a message back to AppDelegate when I'm in the sub window operating via it's controller. As an example, say a button is pushed on the sub window nib and you want to modify an ivar controlled in the AppDelegate? seems that that should be simple, but it seems the object models of the parent AppDelegate are not available.

At a lower level, I'm not sure how to make the AppDelegate aware the window closed. I have had luck with getting the sub window controller to be aware that it's closing via the windowWillClose notification - this works when I set the delegate of the sub nib to the sub window controller. But still the parent controller AppDelegate is not aware and I do not know how to tell the AppDelegate from the sub window controller to do something.

Is the parent AppDelegate made aware via an object message? if so, how? Can the sub nib have a different owner and delegate? is it possible to set the second nib to point to its controller as owner and point back to the AppDelegate as delegate? I tried that but could not figure out how to wire it in connections.

To sum it up, how can I take an action in the AppDelegate after my sub nib via sub controller closes. Any help would be much appreciated.

1

1 Answers

3
votes

You can use a notification to inform your AppDelegate to take an action. You can make your own notification if you need to, or use an existing one like the WindowWillClose notification. You simply have to tell the NSNotification center that the AppDelegate wants to receive the message, like this:

[[NSNotificationCenter defaultCenter] addObserver:myAppDelegate
    selector:@selector(subWindowClosed:)
    name:NSWindowWillCloseNotification
    object:subWindow]

This will call your -subWindowClosed: method on the app delegate when the subwindow closes.