You always did it wrong. MVC is not MASSIVE View Controller. Here is why

Islom Babaev
2 min readFeb 15, 2021
Episode from “The Hobbit: An Unexpected Journey”

Overwhelming majority of iOS engineers refrain from using MVC in their projects because ViewController.swift file gets larger and larger as the layout becomes more complex.

Even if you are not using storyboard to do your iOS projects, we can see how it affects readability of VC.swift files if layout is done programmatically.

Adding more and more views to our screen deteriorates VC’s clarity and manageability. ViewController.swift was not intended to lay out our views on the first place!

If VC becomes cumbersome because of bunch of view objects, why not move those views into dedicated View.swift file?

As we have moved all the redundant logic from the view controller, let’s examine how VC should be structured.

One of the major methods which must be overridden is loadView(), it helps us load our view. How simple it is! Doesn’t it look clean without all those buttons, labels, views and etc.? Never make a mistake by laying out your views in VC.swift file!

TL;DR

  1. Use separate swift file for your views
    Never lay out views in ViewController.swift file. Keep VC’s logic simple and concise without redundant code.
  2. Use loadView to load newly created view
  3. Break larger views into smaller ones
    Breaking down larger views into smaller ones enhances readability, clarity and reusability.

Next topic to be covered is the separation of button action callbacks from a View to View Controller. Stay tuned!

Original idea is taken from my mentor Nodir Y.

--

--