Mvc – Massive View Controller – IOS – Solutions

iosmvcobjective c

I'm sure every new iOS developer has the following problem: The View Controllers get very fast crowded with code for various purposes, easily getting to 500 + lines of code.

This is how it looks like for two basic and common screens:

1) The Form Screen:
enter image description here

2) The Table View Controller Screen
enter image description here

So far I have read about two different solutions:

  1. First Solution: https://bendyworks.com/single-responsibility-principle-ios/.
    This is based on Notifications, it completely separates the View Controller from the (Intentions) View Model and thus reduces code in the View Controller.
    I think that it has the downside of breaking code, similar to Go-To structures.
    It looks like this:
    enter image description here

  2. The second solution keeps the same crowded View Controllers (button actions are executed inside VC and so on). but uses libraries like TPKeyboardAvoiding, BlocksKit or other solutions most of them based on categories. With this second solution, code is drastically reduced but the view controller still has a lot of responsibility.

What do you think about these solutions? Which is better? Is there a better one?

Best Answer

We can use MVVM to resolve this issue.

The Model-View-ViewModel, or MVVM pattern as it’s commonly known, is a UI design pattern. VM takes all logic about preparing model data for UI from VC.

Example:
You have got model object with some fields, you want to format some of them, make calculation and combine them.

In MVC case all that logic located in ViewController.
In MVVM you move all of it from VC to VM.

VM will prepare all data for UI and VC just sets it like this.

(in VC class)

self.descriptionLabel = self.viewModel.textForDescriptionLabel;

Tutorials and topics:

Related Topic