Anyway, how can I have multiple views
in one xib and initialize them
properly? Like if I want to display
one of my custom views in the center
of the screen, how would I do that?
You can have as many UIView
s as you want in your xib. Usually you'll make one the "primary" view -- the one that's hooked up to the File Owner's view property (i.e. your view controller) -- and the rest can sit in the xib at the same level. You can hook them up to other properties in the view controller as well.
You could make these views as subviews for the main view. (Few, that's a lot of view). Let's say you have two views, FooView
and BarView
. You want FooView
to be present at startup. So you just set BarView
to be hidden in IB. Then your view controller might look something like this:
@interface MyViewController : UIViewController
{
FooView* fooView;
BarView* barView;
...
}
...
@property (nonatomic, retain) IBOutlet FooView* fooView;
@property (nonatomic, retain) IBOutlet BarView* barView;
...
Then, inside your code, when you want to change views, just use the setHidden:
method to hide one and show the other.
Alternatively, if I separate the views
into multiple xibs, how can I have
them reference the same controller
object for their IBOutlets/IBActions?
You can have other xibs sharing the same view controller. I've done it lots myself. Just set the File Owner to the appropriate class.