Avoiding Massive View Controller using Containment & Child View Controller

Alfian Losari
Dec 22, 2018 · 5 min read
Containment & Child View Controller Diagram

You can also read this article in my Xcoding With Alfian blog website using the link below.

View Controller is the component that provides basic building block that we use as a foundation to build application in iOS development. In Apple MVC world, it sits as a middle man between the View and Model, acting as an orchestrator between both of them. It starts with the controller acting as an observer that reacts to Model changes, updates the View, accepts user interaction from the view using the Target Action, and then updates the Model.

Apple MVC Diagram. Courtesy of Apple Inc.

As an iOS developer, there are many times that we will face an issue of handling Massive View Controller, even if we use architectures like MVVM, MVP, or VIPER. Sometimes, the View Controller has too many responsibilities to handle in one single screen. It’s violating the SRP (Single Responsibility Principle), creating tight coupling between modules, and making it hard to reuse and test each of the components.

We can take the app screenshot below as an example. You can see there are at least 3 responsibilities in one single screen:

If we are going to build this screen using Single View Controller, it is guaranteed that the view controller will become very massive and bloated as it handles too many responsibilities in one single view controller.

How can we solve this problem? One of the solution is using View Controller Containment and Child View Controller. Here are the benefits of using this solution:

You can view the complete project source code in the GitHub Repository below.

Composition of the View Controllers using Storyboard

View Controller Compositions using Storyboard

According to the above storyboard, here are the View Controllers that we use to build our Filter screen:

The Movie List View Controller

This view controller has the responsibility to display the list of Movie model that is exposed as an instance property. We are using Swift didSet property observer to react to changes in model, and then reload the UITableView. The cell displays the title, duration, rating, and genre for the Movie using default subtitle UITableViewCellStyle.

The Filter List View Controller

The Filter List displays the MovieFilter enum in 3 separate sections: genre, rating, and duration. The MovieFilter enum itself conforms to Hashable protocol so it can be stored inside a Set uniquely using the hash value of each enum and its properties. The selections of the filters are stored under an instance property with Set containing the MovieFilter.

To communicate with other object, a delegate pattern is used using the FilterListControllerDelegate. There are 3 methods for the delegate to implement:

Integrating inside the Container View Controller

In the ContainerViewController, we have several instance properties:

When the viewDidLoad is invoked, we call the method to setup the Child View Controllers. Here are several tasks it performs:

For the FilterListViewControllerDelegate implementation, when filter is selected or deselected, the default Movies data is filtered for each respective genre, rating, and duration. Then, the result of the filter is assigned to the MovieListViewController's movies property. For the deselection of all filters, it just assigns the default movies data.

Conclusion

By looking at the sample project, we can see the benefit of using View Controller Containment and Child View Controller in our app. We can divide the responsibilities of single View Controller into separate View Controllers that only has single responsibility (SRP). We also need to make sure that the Child View Controller does not know anything about its parent. For the Child View Controller to communicate back to the parent, we can use the Delegation pattern.

This approach also provides the benefit of loosely coupled modules that can lead to better reusability and testing of each components. It really helps us scale our app as it grow larger and more complex. Let’s keep learning 📖, Merry Christmas🎄, and Happy New Year🎊 to all of you! Keep on Swifting with Cocoa!!😋


Follow us on social media platforms:
Facebook: facebook.com/AppCodamobile/
Twitter: twitter.com/AppCodaMobile
Instagram: instagram.com/AppCodadotcom

If you enjoyed this article, please click the 👏 button and share to help others find it! Feel free to leave a comment below.

AppCoda Tutorials

A great collections of Swift and iOS app development…

Alfian Losari

Written by

Mobile Developer and Lifelong Learner. Currently building super app @ Go-Jek. Xcoding with Alfian at https://alfianlosari.com

AppCoda Tutorials

A great collections of Swift and iOS app development tutorials. To contribute, tweet us @appcodamobile

More From Medium

More from AppCoda Tutorials

More from AppCoda Tutorials

More from AppCoda Tutorials

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade