How do I correctly load an object that is a subclass of NSView using a Xib?
I want it to be loaded dynamically not from the beginning so I made a MyView.Xib From MyD
Here is a way to write the NSView subclass so the view itself comes fully from a separate xib and has everything set up correctly. It relies on the fact that init can change the value of self.
Create a new 'view' xib using Xcode. Set the File Owner's class to NSViewController, and set its view outlet to your target view. Set the class of the target view to your NSView subclass. Layout your view, connect outlets etc.
Now, in your NSView subclass, implement the designated initializers:
- (id)initWithFrame:(NSRect)frameRect
{
NSViewController *viewController = [[NSViewController alloc] init];
[[NSBundle mainBundle] loadNibNamed:@"TheNib" owner:viewController topLevelObjects:NULL];
id viewFromXib = viewController.view;
viewFromXib.frame = frameRect;
self = viewFromXib;
return self;
}
And the same with initWithCoder: so that it will also work when using your NSView subclass in an another xib.