iOS View Controllers Knowledge

Kip Landergren

(Updated: )

My iOS view controller knowledge base explaining view management, subclassing, and child view controllers.

Contents

Overview

View Controllers are specialized objects that manage a single “root view” of which an entire view hierarchy may be built. Management of a view hierarchy involves creation and coordination of all member views, including any user interaction with them.

View Controllers fall into two categories:

While both types manage a view hierarchy, “Content View Controllers” are responsible for a single piece of content without management of child view controllers. “Container View Controllers” present information from child view controllers in its view hierarchy.

Apps make use of multiple view controllers to facilitate navigation and transitions to different sections of app content.

Core Idea

Use subclasses of UIViewController to manage your app’s:

A single view controller manages a view hierarchy through its “root view”, the UIView object assigned to its view property. Your app manages its view hierarchy through the “root view controller” assigned to the UIWindow object. Views and other view controllers may be managed by this root view controller to create your application’s UI.

Multiple view controllers form their own view controller hierarchy via built-in methods of UIViewController to add and remove view controllers as children of others. This separate hierarchy allows iOS to route events like the responder chain.

Key Concepts

Lazy View Loading

Views are loaded lazily: the view property is nil until accessed, after which it is created. UIViewController calls loadView() to give your subclasses a chance to create the the appropriate view hierarchy. By the completion of loadView() your view controller should have a fully loaded and valid view hierarchy.

Subclassing UIViewController

Initialization

Create required data structures, but not view objects.

The designated initializer is:

init(nibName: nil, bundle: nil)

Convenience initializer sample:

convenience init() {
  self.init(nibName: nil, bundle: nil)
  // create any required data structures to put view
  // controller into a known good state
}

loadView()

Create and wire together your view hierarchy, ensuring the root view is assigned to .view. Do not call super.

viewDidLoad()

View hierarchy exists in a valid state. Load any data necessary for display, and add any initial constraints.

Adding a Child View Controller

Steps:

Table View Controllers

Subclasses of UITableViewController manage a view with tabular data.

Pairing with Fetched Results Controller

A table view controller may have an instance of NSFetchedResultsController to display results from Core Data. This class has the ability to provide information about sections and results that the table view controller needs for display, and can pass information via its NSFetchedResultsControllerDelegate on when underlying data has changed.

Related Documents