3
votes

I have a nib file in which I load at a certain point in my application. Would it be legal for me to link a NSWindow IBOutlet from my AppDelegate to the 2nd nib file's window? In other words, my IBOutlet is not being connected to the MainMenu xib file that Xcode creates on default. If this was legal, can I have access to the NSWindow's frame and other features?

3

3 Answers

6
votes

Yes you can do that. In your second nib file, I would use a NSWindowController as the file's owner to the nib. Then in your AppDelegate, create an instance of the NSWindowController and then load the nib. From there, you can inspect the properties of the window owned by NSWindowController or do whatever you want with the window.

Here is an example

@interface MyAppDelegate : NSObject 
{
    NSWindowController *myWindowController;
}

@end

@implementation MyAppDelegate

- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
{   
    myWindowController = [[NSWindowController alloc] initWithWindowNibName:@"MySecondWindow"];

    [[myWindowController window] center];
    [[myWindowController window] makeKeyAndOrderFront:self];
} 

@end
1
votes

In your second nib, set the File's Owner to be your app delegate class. Then attach the outlets as needed within IB. At run time, call [NSBundle loadNibNamed:owner:] and be sure to pass self as the owner.

0
votes

Yes, this would be legal as long as the App Delegate is the File's Owner of the nib you are loading. That said, if you unload the nib later, you have to make sure that all top level objects in the nib are properly released (otherwise you'll create a memory leak).