Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Custom View Controllers to manage different portions of the same view hierarchy

The View controller programming guide states this regarding view controller's usage:

Each custom view controller object you create is responsible for managing all of the views in a single view hierarchy. In iPhone applications, the views in a view hierarchy traditionally cover the entire screen, but in iPad applications they may cover only a portion of the screen. The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple custom view controllers to manage different portions of the same view hierarchy. Similarly, you should not use a single custom view controller object to manage multiple screens worth of content.

I understand that if we use multiple custom view controller's to control the parts of a view (i.e. a view controller to manage subViews of a main view which in turn is managed by a view controller) the default methods like:

didReceiveMemoryWarnings
viewWillAppear
viewWillDisappear
viewDidUnload

etc. etc. will not be called.

Apart from this, is there any other solid reason why we should not be using multiple view controllers to manage the respective subviews of a view?

The documentation also provide an alternative solution which reads as:

Note: If you want to divide a view hierarchy into multiple subareas and manage each one separately, use generic controller objects (custom objects descending from NSObject) instead of view controller objects to manage each subarea. Then use a single view controller object to manage the generic controller objects.

But there is no mention as to why multiple view controllers should not be preferred. My question is:

Why should not we prefer it this way?

I am concerned because I prefer using UIViewController's subclass to manage my views since I load them from nib each time and I segregate nibs for each view controllers. It becomes easy to cater the changes in later stages of the project. Is this wrong? Should I necessarily change my programming style, or is it ok if I go ahead with this approach?

Thanks,

Raj

like image 490
Raj Pawan Gumdal Avatar asked Dec 02 '10 09:12

Raj Pawan Gumdal


People also ask

What are used to control how a view appears relative to its parent view and other sibling views?

Constraints are rules that specify how to size and position each view relative to its parent or sibling view, and they ensure that your views automatically adapt to different environments and devices.

What type of view controller is used in apps consisting of a number of view controllers representing different app sections?

A container view controller embeds the content of other view controllers into its own root view. A container view controller may mix custom views with the contents of its child view controllers to facilitate navigation or to create unique interfaces.


1 Answers

Well, I'd say "as long as it works", you can keep on doing like you do ! But to keep things "cleaner", I'd use my own objects. Since ViewControllers are designed with other general features in mind (like working with navigation controllers and tab bar controllers), which makes it a bit "heavy" for a simple usage, like you do. Plus, like you mentioned, some events are only called when the viewController's view is added to the main window.

Can't you use your own objects with Interface Builder ? If you create one (or several) UIView IBOutlet(s), it should work the same.

like image 181
Julien Avatar answered Oct 08 '22 15:10

Julien