The view controller family (iOS 4)

So far in the last two topics we haven’t strayed far from the most fundamental building block of the SDK: the view, whether a UILabel, a UIWebView, or a Ullmage-View. Ultimately, the view is only part of the story. As we mentioned when we looked at iOS, views are usually connected to view controllers, which manage events and otherwise take the controller role in the MVC model. We’re now ready to begin a three-part exploration of what that all means.

In this topic, we look at basic view controllers that manage a single page of text. With that basis, we can examine events and actions in topic 6, correctly integrating them into the MVC model. Finally, in topic 7, we’ll return to the topic of view controllers to look at advanced classes that can be used to connect several pages of text.

Over the course of our two view controller topics (5 and 7), we’ll offer code samples that are a bit more skeletal than usual. That’s because we want to provide you with the fundamental, reusable code that you’ll need to use the controllers on your own. Consider topics 5 and 7 more of a reference—although a critical one. You’ll make real-world use of the controllers in the rest of this topic, including when we look at events and actions in topic 6. Right now, though, let’s examine the available view controllers.

When we first talked about view controllers in topic 2, we mentioned that they come in several flavors. These run from the bare-bones UIViewController, which is primarily useful for managing autorotation and for taking the appropriate role in the MVC model, to the more organized UITableViewController, on to a few different controllers that allow navigation across multiple pages.


All of these view controllers—and their related views—are listed in table 5.1.

Table 5.1 There are a variety of view controllers, giving you considerable control over how navigation occurs in your program.

Object

Type

Summary

UIViewController

View

controller

A default controller, which controls a view. Also the basis for the flipside controller, which appears only as an Xcode template, not as a UIKit object.

UIView

View

Either your full screen or some part thereof. This is what a view controller controls, typically through some child of UIView, not this object itself.

UITableViewController

View

controller

A controller that uses UITableView to organize data listings.

UITableView

View

A view that works with the UITableViewController to create a table UI. It contains

UITableCells.

UITabBarController

View

controller

A controller that works with a UITabBar to control multiple

UIViewControllers.

UITabBar

View

A view that works with the UITabBarController to create the tab bar UI. It contains

UITabBarItems.

UINavigationController

View

controller

A controller used with a UINavigationBar to control multiple UIViewControllers.

UINavigationBar

View

A view that works with UINavigationController to create the navigation UI.

Table 5.1 There are a variety of view controllers, giving you considerable control over how navigation occurs in your program.

Object

Type

Summary

Flipside controller

View

controller

A special template that supports a two-sided UIViewController.

tmp12-109

View

controller

Modal view controllers that allow interaction with sophisticated user interfaces for the Address Book and the photos roll.

As we’ve already noted, we’ll be discussing these view controllers in two different topics. Here, we’ll look at the single-page view controllers: UIViewController and UITableViewController. In topic 7, we’ll examine the multipage view controllers: UITabBarController, UINavigationController, and the flipside controller. This is a clear functional split: the single-page controllers exist primarily to support the controller role of the MVC model, whereas the multipage controllers exist primarily to support navigation and may even delegate MVC work to a simpler view controller lying below them. (As for the modal controllers, we’ll get to them when we cover the appropriate topics in topics 8 and 11.)

So far, you’ve been programming without using view controllers, which are an important part of SDK programming. You could write an SDK program without them, but every SDK program should include them, even if you use a bare-bones view controller to manage the rotation of the screen.

Next post:

Previous post: