Parmanoir

Bindings, Outlets, Target+Action across multiple NIBs

When using multiple NIBs, we need a common object that will share data among them. That object will hold bindings, outlets, target/action shared across NIBs. Here we'll use the Application Controller :

Image:MultipleNIBs.gif

How does a NIB work anyway ? Cocoa defines classes (NSWindow, NSTextField), you define classes in your application (ApplicationController, SomeObject). A NIB is a collection of instances of these classes. Adding Windows, custom objects (like ApplicationController), or controllers is just like calling [[SomeClass] alloc] init] — but the NIB will do it for you.

To be useful, these instances must be linked to an existing object — the File's Owner. When loading a NIB, you specify that object as a parameter : [NSBundle loadNibNamed:@"SecondWindow" owner:self].

If you have only one NIB, you don't have to do a thing — loading will be automatic and the File's Owner will be NSApplication. That's how you can link an instance of your ApplicationController to the delegate outlet of File's Owner, which is the running NSApplication.

But if you have multiple NIBs, that owner is your way to share data. Using ApplicationController as owner is like having that blue box in our first NIB — but it has a different name. And you use it just the same : you can bind to ApplicationController's data, define outlets and actions using that File's owner object which is in fact your ApplicationController.

Watch those outlets

If you put your Preferences window in another NIB and define an outlet to it in ApplicationController, it will be initialized only when you load the NIB. Yes, that's obvious for something like the preferences window — but it might not be for whatever else you do. When you load a NIB at runtime, bindings and outlets are setup just then : the first NIB has no idead about the second one.

If you load a NIB multiple times, it will create a new copy of your NIB's objects each time. This is what happens with NSDocument : you get a fresh copy of your window defined in IB. If your NIB defines an outlet to ApplicationController, it will be overwritten with the new copy at each load. Be careful there.

Image:iconZip.png NibRuntimeLoad.zip

Cocoa Bindings


Follow me on Twitter
Planet Cocoa
Cocoa.fr

2011 02 22Distance field
2010 07 202Binding through NSApp
2010 05 122Forwarding invocations
2010 02 272Core Image black fringes
2010 02 21Quickest Way to Shell
2010 02 08Who's calling ?
2009 09 2138 ways to use Blocks in Snow Leopard
2009 08 182Bracket Mess
2009 08 124Taming JavascriptCore within and without WebView
2009 04 15Debugging with Activity Monitor
2009 03 25How Core Image Color Tracking works
2009 03 1510Custom NSThemeFrame
2009 03 10Which framework is running ?
2009 03 074CoreUI can paint pretty big
2009 02 18Localization with functions
2009 01 30Did you forget to nest alloc and init?
2009 01 16JSCocoa on the iPhone
2009 01 11Mixing WebView and JavascriptCore
2009 01 09Badge overflow
2009 01 09Find your Garbage Collection leaks with Instruments

Powered by MediaWiki