Matt S.
Matt S.

Reputation: 13773

Add NSView from different nib

How can I add a subview when the new view is in a different xib file?

The class for the different nib is an NSViewController and I'm using self = [super initWithNibName:@"NewView" bundle:nil]; to load the nib

Can I just do something like:

NewView *nv = [NewView new];
[oldView removeFromSuperView];
[mv addSubview:[nv theView]];

or do I have to do something different

Upvotes: 1

Views: 906

Answers (1)

Rob Keniger
Rob Keniger

Reputation: 46028

Yes, that's correct, providing NewView is a subclass of NSViewController. Having said that, you shouldn't name a controller class NewView, as it's not a view. Your subclass of NSViewController should really be named NewViewController.

You can also do this:

[[oldView superview] replaceSubview:oldView withView:nv];

Of course, this assumes that your NewView nib file has its File's Owner set your subclass of NSViewController.

Upvotes: 1

Related Questions