How to Use View Controllers to Develop Your iOS App - dummies

How to Use View Controllers to Develop Your iOS App

By Jesse Feiler

The storyboard — the whiteboard, so to speak, is where you lay out the flow of the elements, or design pattern, of your iOS app. The controller objects contain the code that connects the app’s view objects to its model objects. Whenever the view needs to display something, the view controller goes out and gets what the view needs from the model.

Similarly, view controllers respond to controls in your Content view and may do things like tell the model to update its data (when the user adds or changes text in a text field, for example), compute something, or change the view being displayed (like when the user presses the Detail Disclosure button on the Music app to find out more about a song).

View controllers are the objects that control what is displayed and that respond to user actions. They are the heart and soul of the storyboard.

image0.jpg

A view controller is often the (target) object that responds to the onscreen controls. The Target-Action mechanism is what enables the view controller to be aware of any changes in the view, which can then be transmitted to the model.

Imagine that an iPad user launches the RoadTrip app. On the left side (or in a popover), she sees a Table view and on the right side a Detail view (the Weather, for example). The user may tap an entry (cell) in the Table view to display events.

The Events controller is then launched and sends a message to the appropriate method in the model to get the events. The model object returns a list of URLs, and so on. The controller then delivers that information to the view, which promptly displays the information to the user.

If your imaginary user now launches the RoadTrip app on the iPhone (or iPod touch), the user will see a full-screen table view. The screen will shift to a detail view when the user taps a cell in the table. The same information is available in the iPhone version as in the iPad version, but the user interface has to be slightly different.

You will be pleased to know that you can build one set of Objective-C classes that works with both the iPhone and iPad storyboard files, so you only need one set of code to build your Universal app.

The sequence of events is as follows:

  1. A message is sent to that view’s view controller to handle the request.

  2. The view controller’s method interacts with the Trip model object.

  3. The model object processes the request from the user for the current events.

  4. The model object sends the data back to the view controller.

  5. The view controller sends the data to the view to display the information.

View controllers have other vital iOS responsibilities as well, such as the following:

  • Managing a set of views: This includes creating the views as well as flushing them from memory during low-memory situations.

  • Responding to a change in the device’s orientation: If, say, the user causes the iPad to switch from landscape to portrait orientation, the view controller responds by adjusting its views to match the new orientation.

  • Creating a Modal (not model) view: A Modal view is a child window that displays a dialog that requires the user to do something (tap the Yes or Cancel button, for example) before returning to the app.

    You use a Modal view to ensure that the user has paid attention to the implications of an action (for example, “Are you sure you want to delete all your contacts?”).

  • Display a popover: A popover is a transient view that is shown when people tap a control or an onscreen area. Popovers are used in a variety of ways, including displaying the Master view when a Split View app is in Portrait orientation, or displaying additional information about a selection. They are only available on the iPad.

  • Respond to user input and navigation: While the view processes a touch using the Target-Action pattern, it is almost always the view that is the target of the action — responding to the touch appropriately (like having the image of a ’59 Cadillac Eldorado Biarritz convertible drive right up the center of the screen — sound effects included).

View controllers are also typically the objects that serve as delegates and data sources for Table views as well as for other kinds of framework views.

In addition to the base UIViewController class, UIKit includes the following:

  • Subclasses such as UITabBarController (to manage the Tab bar)

  • UITableViewController (which you use to manage Table views)

  • UINavigationController (which implements navigation back and forth between view controllers)

  • UIPageViewController (to allow users to navigate between view controllers using the specified transition)

  • UIIMagePickerController (to access the camera and Photo library on the iPad)

  • UISplitViewController (which you’ll be using on the iPad only to display the side-by-side views).