2

I'm new to Cocoa and am having trouble splitting my nib file into multiple nib files.

My MainMenu.nib contains a Tracker Controller object, which is a subclass of NSObject. It has an outlet to a Show Tracker menu item in my main menu.

My TrackerWindow.nib has the File's Owner class set to TrackerController, and has outlets to the window and view in that nib file.

I'm not sure how to make the File's Owner of the second nib be a proxy for the instantiated TrackerController in the first nib. (I believe I need the TrackerController instance in the first nib so that I can use IB to set the menu item outlet.)

Am I doing it wrong? If so, how can I use IB to set outlets for the same object in multiple nib files? If not, how can I make the File's Owner of the second nib point to the TrackerController I've already instantiated in the first nib?

nfm
  • 19,689
  • 15
  • 60
  • 90

1 Answers1

2

This is written from the perspective of an iOS developer (using view controllers). I'm not sure how Mac OS X differs but it shouldn't be difficult to transplant the ideas.


The simplest way to set the File's Owner of a nib is to provide it as an argument to loadNibNamed:owner:options::

[[NSBundle mainBundle] loadNibNamed:@"Tracker" owner:trackerController options:optionsDict];

The snippet above assumes that trackerController is an instance of UIViewController. If it isn't, use the following solution instead.


Instead of using initWithNibName:bundle:, create a TrackerViewController as follows (where trackerController is a reference to the existing TrackerController object):

NSDictionary *proxyDict = [NSDictionary dictionaryWithObject:trackerController forKey:@"trackerController"];
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:proxyDict forKey:UINibExternalObjects];
TrackerViewController *trackerViewController = [[[TrackerViewController alloc] init] autorelease];
[[NSBundle mainBundle] loadNibNamed:@"Tracker" owner:trackerViewController options:optionsDict];
// Display trackerViewController

Create an External Object with an Identifier of trackerController in Tracker.nib and connect your outlets/actions to this object.

titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
  • Thanks, that set me on the right path! As I had already instantiated a `trackerController` in my `MainMenu.nib`, I used the following: `[NSBundle loadNibNamed:@"TrackerWindow" owner:self.trackerController];`. – nfm May 17 '11 at 01:12