0

Apple's resource programming guide (RPG) states "it is better to distribute components across multiple nib files."...

therefore, i have an associate window nib (Nib 2) that has an nsobjectcontroller that needs to be linked (selection self) to a nsarraycontroller in the main document window nib (Nib 1).

i need to share a common instance (either the nsarraycontroller in nib 1 or nsobjectcontroller in nib2). I can add a custom object in Nib 1. and set the File's Owner to that type of custom object. however, each nib instantiates their own instance.

is there a method of setting which nib an object was instantiated, or declaring an external reference.

i also "Make the File’s Owner the single point-of-contact for anything outside of the nib file" (RPG). Which is a NSWindowController.

Thanks in advance.

Gabe Rainbow
  • 3,658
  • 4
  • 32
  • 42

1 Answers1

0

You probably want to make the owner of NIB1 responsible for instantiating NIB2. This will allow it to be the owner of both NIBs. In the common case, it might look something like this:

// In the interface...
@property (nonatomic, readwrite, retain) NSArray* nib2TopLevelObjects;

// In the implementation...
- (void)awakeFromNib
{
    NSNib* nib2 = [[[NSNib alloc] initWithNibNamed: @"NIB2" bundle: [NSBundle mainBundle]] autorelease];
    NSArray* tlo = nil;
    [nib2 instantiateWithOwner: self topLevelObjects: &tlo];
    self.nib2TopLevelObjects = [tlo retain];

    // Do other stuff...
}

- (void)dealloc
{
    [_nib2TopLevelObjects release];
    [super dealloc];
}

At the end of this, NIB2 will have been instantiated with NIB1's owner as it's owner as well, and NIB2 will have plugged its objects into the shared owner (be sure not to plug things into the same outlet from both NIBs.)

All that said, I'm not sure this is necessarily the right pattern to use here. If these windows are both views upon the same document, you should probably make an NSWindowController subclass for each window and override -[NSDocument makeWindowControllers] to instantiate them. (The NSWindowController will be the "File's Owner" for each NIB.) Having the document NIB's owner be the NSDocument subclass is a "short cut" for simple situations. Once you need multiple windows, NSWindowControllers are the way to go.

Each NSWindowController can get back to the document via -document and the NSDocument subclass can coordinate state between the different NSWindowControllers. This is a cleaner approach, and avoids all the shenanigans with clobbered IBOutlets, etc.

For your specific case, I could see having a property like sharedArrayController on the NSDocument subclass that gets the NSArrayController from NIB1 during -makeWindowControllers, and re-vends it. Then you can then access it from NIB2 by binding to File's Owner > document.sharedArrayController.selection.

ipmcc
  • 29,581
  • 5
  • 84
  • 147