Composition and child view controllers
As you saw previously, you should always pay attention to the view life cycle and never rush it. Another thing that we need to pay attention to is the size of our view controller. Smaller view controllers are beneficial for the following:
- Reusability: Smaller view controllers are reusable, as they tend to be very specialized.
- Testability: A small view controller has a smaller footprint and a smaller API. Having a smaller API makes it easier to test.
- Maintainability: Isolating features and components always helps with the separation of concerns.
All of these features come for free when you compose your view controllers accordingly.
Composing view controllers implies using a custom container view controller. The container view controller will be responsible for maintaining the hierarchy of controllers, as well as the views provided by those child view controllers.
Adding child view controllers
Adding a child view controller is possible with the func addChild...