Home
+91 99674 50288
info@appmonks.net
×

Write Better MVC in your iOS App!


In our previous iOS blog post we make a strong suggestion that we need to stop using storyboards and shift towards a complete code based UI development.

A lot of developers have been debating over abandoning storyboards for relatively small projects that could be done in few months if we use storyboards. So a strong heres a small tip for you; if you still want to use storyboards.

Just make sure that you have separate storyboard file each screen/task. And make each storyboard as independent as possible. And for transitions to other screens, just use segues to a storyboard reference or just do it programatically.

If you start using this approach you will soon realise that the code is getting worst over time. First you have lots of code to perform transitions between screen manually. Then you have to write all code for that screen on that view-controller. And before you even realise, you have your ViewController already crossing 100s of lines of code. A Debugging and reading Nightmare!

So, let’s just follow MVC.

Yeah you might say that I follow MVC already with the standard Apple’s MVC way. But, wait a min! Do u actually follow MVC?

MVC says that Model handles data, View handles the UI and controller is supposed to be, well, the controller. But often time that is not our code. Our view controller has all the code crammed into it. Which means the ViewController is having the responsibilities of all, Models, View and the Controller.

Thats just wrong!

Let’s try and refactor and TableViewController to make it better MVC.

Let’s say you have a ViewController -> OrderTableViewVC. We would have data source and delegate right inside the VC, and UI changes also inside the VC. And this is just messed up.

Step 1, Just make sure that you model is completely outside the ViewController. So, no matter what you do, the Model is handling everything that is related to the data.

Step 2, If you have to manipulate the View, write custom UI components, create inherited custom UIKit classes.

Step 3, Remove the data source from View Controller file. Create a separate NSObject that handles the data source.

Step 4, last but not the least, only make connection to the data, updates to view components in the VC.

If you follow all steps in the right way, you will definitely be creating more files. But all the files will be short, upto the mark; and definitely be following the Single Responsibility principle.

And since you code is divided properly based on their responsibility, you will have indeed written better MVC code. Stay tuned for more….


Mail
casino