views:

138

answers:

1

For my application, I have one UIViewController and about 8 UIViews. The views are all properties of the view controller, linked via the Interface Builder (IBOutlet). So when the view controller loads, all of the views are also loaded, and I have built-in methods to switch back and forth between the different views.

Is it bad to have them all linked to one view controller -- should each view have its own view controller? Because they're all linked to one, I'm assuming they're all in memory at the same time and are never released because the view controller itself is never released.

What is the standard practice for this?

+3  A: 

If you have a bunch of views that will always be on-screen at the same time, then they should be controlled by one UIViewController.

If you have a bunch of views that will alternate between completely controlling the screen, then each view should have its own UIViewController.

If you have a single view that's always on-screen that delegates part of the screen to another view that can change, then you should have a UIViewController to manage the main view as well as one UIViewController per subview.

(Any time you have a view that can sometimes be on-screen and sometimes be off-screen, you should probably be using a UIViewController to manage its lifespan.)

John Calsbeek
Ahm.. How exactly would you use a separate UIViewController to manage a _subview_? From the UIViewController documentation: "You use each instance of UIViewController to manage a full-screen view."
Daniel Rinser
There are many subclasses of `UIViewController` that don't manage full-screen views—like `UITabBarController`. It can still manage the lifespan of a view even if that view is not full-screen.
John Calsbeek
Great explanation John, thanks!
James Skidmore