I have one main view with an associated controller. I have some rather complicated toolbars that I need to switch in and out depending on user interaction.
To keep things simple I manage the toolbars with my main view controller, rather than having all sorts of intertwined dependencies or really deep delegate chains.
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?
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 as many
UIViews 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,
FooViewandBarView. You wantFooViewto be present at startup. So you just setBarViewto be hidden in IB. Then your view controller might look something like this:Then, inside your code, when you want to change views, just use the
setHidden:method to hide one and show the other.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.