开发者

Cocoa - Link IBOutlet to Separate Nib

I 开发者_Python百科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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜