Cocoa: Multiple NIB application

Why

  • Windows lazy-loading: shorter launch time
  • Easier NIB localization
  • Clean and tidy

Example

A good example from Apple:

/Developer/Examples/InterfaceBuilder/SimpleMultiWindow

Here are some missing notes:

SecondMenu.nib

No need to create a controller's instance here. Outlets and actions connections have to be made to File's Owner, which is a SMWSecondWindowController (by setting its custom class).

That's understandable from the way SecondWindow.nib is initialized:

_controller = [[SMWSecondWindowController alloc] initWithWindowNibName:@"SecondWindow"];  

MainMenu.nib

File's Owner has to have its delegate outlet set to the controller instance SMWAppDelegate.

So SMWAppDelegate is the application delegate, and then can this code from SMWSecondWindowController.m work:

[[NSApp delegate] answerQuestion:[questionField stringValue]];

Bindings

How to keep bindings working across multiple NIB files? One solution is making your main controller instance the application delegate (like with SMWAppDelegate above). So you can add this accessor to your window controller subclass (SMWSecondWindowController in the example above):

- (Controller *)controller
{
        return [NSApp delegate];
}

Then you can bind anything from your new NIB to File's Owner and set the keyPath to something like that:

controller.addressesArrayController.selection.postalCode

In this example, addressesArrayController is an oulet of your main controller class connected to an NSArrayCOntroller.

Feedback