开发者

Cocoa Loading a ViewNib

When creating a nib, I have 2 types to create, a WindowNib or a ViewNib. I see the difference is,开发者_开发问答 the window nib has a window, and a view.

How do I load a view nib into another window? Do I have to create a WindowController and add a window then load the nib in the window?


Typically, you should have a Controller for each NIB. So in the case of a Window NIB you would have an NSWindowController subclass as the File's Owner. Similarly for a View NIB you would have an NSViewController subclass as the File's Owner. In the case you present the NSWindowController subclass would instantiate the NSViewController subclass (passing it the appropriate View NIB) and then attach the NSViewController's view property to the window's view hierarchy.

Example in your Window Controller's awakFromNib method you would have the following:

- (void) awakeFromNib {
    _viewController = [[MyViewController alloc] initWithNibName: @"MyView" bundle: nil];
    [[[self window] contentView] addSubview: [_viewController view]];
}

You could also place this code in the windowDidLoad method of your NSWindowController subclass.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜