What is a Container View Controller

Assemble and embed UIViewControllers

2nd May 2019

In addition to the view hierarchy, the view controllers also form a hierarchy. Complex screens can be composed of several UIViewControllers to achieve the Massive view controller-Solve problem (huge view controller classes that mix up a lot of responsibilities).

Via storyboard

With the Container view other view controllers can be embedded to split complex controllers:

Programmatically

The following steps must be taken to embed a controller:

  1. Instantiate the view controller, e.g. via Storyboard ID load from the storyboard:

    letviewController = self.storyboard! .instantiateViewController (withIdentifier: "Example")
  2. Get the view of the controller:

    guardletsubview = viewController.viewelse {return}
  3. Insert the ViewController as a subordinate controller in the hierarchy:

  4. Place the view of the subordinate controller in the view of the higher-level controller, if necessary lay out / create constraints:

    view.addSubview (viewController.view)
  5. notification didMove send (willMove takes place automatically when calling addChild):

    viewController.didMove (toParent: self)

Subscribe to blog

Get new articles and tutorials on iOS development emailed to:

additional Information

  • MVC is Not Your Problem
    Lecture by Joachim Kurz at the UIKonf 2018 with recommendations for structuring view controllers.
  • A Better MVC
    Series of blog posts by Dave DeLong on structuring view controllers.