Write Clean Code by Overriding loadView()

Refactor it!

Steven Curtis
The Startup

--

Photo by Atikh Bana on Unsplash

Rather than using a storyboard you can…can. Also avoid a Massive View Controller to boot!

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

You may even have found tutorials that help you put your code in the right place in your MVC model. They might even mention overriding loadView() for such a task, but may not include where we put gestures for buttons and other code that just can’t be done in the UIView subclass.

The general idea here is that constraints (and such should not be in the view controller class. Let us see how this works with an example.

The example

This is going to be one of the most simple Apps that you can imagine. There is a single view, you click a button and move to the next view. That’s it. Except a button to go back to the first view. That would look something like the following:

The God class

Imagine we have a rather simple App, where pressing a button moves from one UIViewController to another one.

--

--