简体   繁体   中英

Cocoa - Link IBOutlet to Separate Nib

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?

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

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM