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 MyDocument.m I did:
MyView *myView = [[MyView alloc] initWithFrame:...];
[NSBundle loadNibNamed:@"MyView" owner:myView];
[someview addSubview:myView];
...
and the background is fine (it calls drawrect: and it's drawn as expected)
but all the buttons I put on the xib won't appear.
I have checked, and they are loaded BUT their superview is not the same object as myView
.
Why is this? I think I am missing something in the Xib but I don't know exactly what. In other words: How do I make sure that the root view in my xib is the same object as file's owner?
I wish there was something similar to this for the mac:
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil]; //in iOS this will load the xib
MyView* myView = [ nibViews objectAtIndex:1];
[someview addSubview:myView];
...
Thanks in advance.
EDIT
I think I have realised the origin of the problem...(??)
In MyView class I have various IBOutlet which are connected correctly in IB that is why they load just fine (I can refer them). However there is no IBOutlet for the top view. So when NSBundle loads the nib, the top view gets assigned to some other object. I thought this will happen if I set my top view in IB to be from class:MyView
and put myView
as the owner in [NSBundle loadNibNamed:@"MyView" owner:myView];
but it seems not to be the case.
What am I doing wrong?
MyView
? Are there other top-level objects in the nib file? And what’s the class of the file’s owner? – user557219MyView
the top view in it is currently of classNSView
but I have tried alsoMyView
. Neither worked – nacho4d