Subclassing UIView and displaying multiple instances
I am trying to do the following:
Display an instance of a custom
UIView
. ThisUIView
subclass should load it's content from Interface Builder, because it shows stuff that I don't want to create by hand. My question is: How can I load from Interface Builder in aUIView
subclass?I will eventually animate this v开发者_如何学Ciew out of the screen and release it. (I know how to do that :-) )
GOTO Step 1, i.e creating a new instance of my
UIView
and display it.
Probably a noobish question, but I can't seem to figure it out. I would appreciate some help, Fabian
In iOS 4.0+, create an instance of UINib
referencing your nib file (+nibWithNibName:bundle:
), then instantiate the objects in the nib with -[UINib instantiateWithOwner:options:]
.
If you need to target iOS 3.x, too, call:
[[NSBundle mainBundle] loadNibNamed:owner:options:]
which returns an array of the objects in the nib file.
To load an interface from Interface Builder you need to use a UIViewController. If you don't use interface builder though, you can create a uiview subclass, create your interface, and then create instances of your class. For every class all you need to do is this:
MyView *view = [[MYView alloc] initWithFrame:theFrame];
MyView *secondView = [[MyView alloc] initWithFrame:theFrame];
//do stuff with the views
//release the views
[view release];
[secondView release];
精彩评论