开发者

Decomposing an iPhone user interface -- multiple views in one xib?

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 prop开发者_如何转开发erly? 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?


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 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, 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.


It's actually very simple. You just create a new root-level view in your XIB file and create an IBOutlet for this view in your main view controller.

As far as loading views directly from XIB's, sharing the same view controller, that's a little more sloppy.. you'd essentially need to loop through the items in the XIB file and find the element you're looking for.


One thing to note from this approach is that all the views get initialized when any of the views are needed.

If your views are simple this might not matter, but if you have complex views, this might make your application slow to load.

If the views are somewhat complex, you should split them into different .xib files. In this case the parent UIViewController would have IBOutlets to the other UIViewController subclasses rather than IBOutlets to UIViews. The view controllers will automatically handle the lazy-loading of their respective views.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜