Question on View Controller's view

2 Solutions Collect From Internet About “Question on View Controller's view”

donkim is right but don’t take my word for it 😉

See this post by Rincewind: and any number of older comments from other Apple employees on their forums.

If I’m understanding what you are
saying, you’ve created another view
controller (BlueViewController) and
added its view as a subview of another
view controller? If so, then this is
covered by the docs – specifically to
say not to do that because it is not
A view controller is
meant to manage an entire screen’s
worth of content. If you want to
subdivide your content further, then
it is recommended that you use objects
other than subclasses of a
UIViewController to manage those
subregions and manage those objects
with a UIViewController subclass.

In addition we have the UIViewController class reference which states:

You use each instance of UIViewController to manage a view hierarchy. A typical view hierarchy consists of a root view—a reference to which is available in the view property of this class—and usually one or more subviews presenting the actual content. On iPhone and iPod touch, the root view typically fills the entire screen but on iPad this view may fill only part of the screen. In both cases, the view controller is responsible for managing the entire view hierarchy, including all subviews.

and also the View Controller Programming Guide for iOS:

The root view acts as the container for all other views associated with your view controller. You typically define the frame for this view to match the size of the application window, which itself should fill the screen. However, the view controller also adjusts the frame size as needed to accommodate the presence of assorted views, such as the system status bar, a navigation bar, or a tab bar.

Unfortunately none of those are as explicit as I would like about why UIViewController makes these assumptions and when it might be safe to break them. From what I can tell without access to UIKit code somewhere UIKit assumes a single view controller will be responsible for a given screen worth of content. In some cases it therefore sends messages like -viewWillAppear: or -didRecieveMemoryWarning to only one (or maybe only some) view controllers if you have multiple view controllers visible in a single window. It should be possible to demonstrate this in at least a couple of different ways though I haven’t tested all of these recently so I’m going off of other user’s reported problems.
Nested view controllers will not receive -didReceiveMemoryWarning
Multiple view controller’s view will not be rotated if rotation occurs while a modal view controller is presented and the modal is then dismissed.
View controllers’ views are sometimes expected to fill the window and be opaque so the content drawn in regions of the window they do not cover can be undefined. Trying to present a modal view controller which did not fill the screen would not render underlying views, at least in iOS 3.1.x or whatever the last version I tried was.

To try to directly answer hmthur’s question. A UIViewController should have a single root view (its ‘view’ property) which fills the window and which can handle being resized to fit status bars, tab bars, and so on. That controller may have any number of sub views of that root view and properties which reference those sub views.

As a further update consider this thread on “Creating container UIViewController subclasses?” on the Apple developer forums (discussing setting parentViewController in order to build custom container view controllers).

All UIKit ivars are considered private and their direct access is unsupported, including access through setValue:forKey:. The method that sets this ivar is also private.

As for this ivar [parentViewController], I will warn you that its behavior is likely to change.

Unfortunately I don’t have any good suggestions for alternatives, though for what it’s worth we do recognize the difficulty in creating container view controllers.

According to Jonah in this question, a view in a UIViewController should be assumed to fill up the entire screen. I’ve written apps that have a view of a UIViewController not take the entire screen and it’s worked great for me.

For your second question, any UIView can add other UIViews via the addSubview: method. So yes, in a sense, there can be multiple views in the UIViewController (though there will only be the one view property).