7.3 · View controller lifecycle, containment, presentation and transitions

The view controller is UIKit’s unit of screen. Its lifecycle methods are the most-asked UIKit topic at every level, and presentation and transitions are where seniority shows.

Mental model. A controller’s view is created lazily, appears and disappears in paired will/did steps, and is managed by parents (containers) that forward those events. Most bugs are doing something in the wrong step, or forgetting the parent.

Lifecycle and containment

What runs when, and how child controllers fit in.

  1. What is the view controller lifecycle, and what belongs in each method? 7.3.1 · L1
    The view is created lazily the first time something asks for view: loadView creates it (override only to build the view yourself, and never call super if you…
  2. What is view controller containment, and what is the correct way to embed one controller in another? 7.3.2 · L3
    A container controller (UINavigationController, UITabBarController, or your own) displays other controllers’ views inside its own. Containment tells UIKit…

Presenting and transitioning

Modal presentation, and custom animations and interaction.

  1. How does modal presentation work, and how do the presentation styles and sheets differ? 7.3.3 · L3
    Presenting is present(_:animated:completion:) on a controller, which becomes the presenting controller; the new one is the presented controller…
  2. How do you build a custom view controller transition, including an interactive one? 7.3.4 · L5
    A transition has three roles. A transitioning delegate (UIViewControllerTransitioningDelegate for modals, UINavigationControllerDelegate for push and pop) is…

More in UIKit