iOS Dev Nugget 174 View Controller Containment to Avoid Growing a Fat View Controller Base Class


Need to run a code review on your codebase? Hire me

In any sizable project, it's easy to end up with a view controller hierarchy with a base class that mostly act as a dumping ground for shared code that is used across one or more view controller subclasses. For many reasons, include the Single Responsibility Principle, a fat base class isn't desirable.

Soroush Khanlou has a great writeup about how to use view controller containment (available since iOS 5) to reduce the size of your view controller base class called Many Controllers Make Light Work. The basic idea is view controller containment lets you compose smaller view controllers that are each dedicated to a single task, and because child view controller's view can be configured to be invisible and still get appearance callbacks such as -viewDidAppear:, you get nice hooks to wire up code.

He gives examples such as extracting code that shows progress/loading views (aka. SVProgressHUD) and app analytics.

Check out the article.

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No



Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues