UIViewController

The UIViewController class provides the fundamental view-management model for iPhone. You use instances of UIViewController, and its subclasses, to manage tab bars, navigation bars, and the application views you want displayed when items are selected. The UIViewController class also supports modal views and rotating views when the orientation changes.

The UIViewController class is a superclass for all controllers that manage a full-screen view. For example, a tab bar controller manages a tab bar and the view above it used to implement radio interfaces—clicking on tab bar buttons toggles the view above the tab bar. A navigation controller manages a navigation bar and the view below it used to navigate hierarchically. These specialized view controllers hide the complexity of using the UITabBar and UINavigationBar classes directly.

You primarily subclass the UIViewController class to create your own custom view controllers that manage your full-screen views. You can create a single view controller and add its view to a window just for the benefit of autorotation. Or add the view controller to a tab bar controller or navigation controller and let those objects manage your full-screen views. What methods you use to configure your view controller depends on how you plan to use it.

All subclasses of UIViewController should implement the loadView method to set the view property. The view you create should be a normal view that can be added to any view hierarchy and resized by the controller. When a view controller is added to a tab bar or navigation controller, its view is resized to fit the available space between the tab bar and navigation bar if they are present. Use the autoresizesSubviews and autoresizingMask properties so that your view and its subviews autoresize gracefully. You’ll need these properties set to autoresize when the orientation changes too.

A view controller pushed onto the stack of a navigation controller should also set some of the properties in “Configuring Navigation Items” to configure a navigation bar when the view controller is the top view controller. Specifically, use the navigationItem property to configure the navigation bar. Read UINavigationController Class Reference for details.

Similarly, a view controller that is added to a tab bar controller should set some of the properties in “Configuring Tab Bar Items” used to update the tab bar when the view controller is selected. Use the tabBarItem property to configure the tab bar. Read UITabBarController Class Reference before using the “Configuring Tab Bar Items” methods.

View controllers also support autorotation—they slide the tab bar and navigation bar out before the interface rotates, and slide them in after the interface rotates. You can optionally augment the animation and even replace the view for different orientations—for example, use a different view for portrait than for landscape orientation. If your view controller supports orientations other than portrait, override the shouldAutorotateToInterfaceOrientation: method to return YES for the orientations it supports.

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: