BLOG

IOS

Lightweight IOS view Controllers

 

Model. View. Controller. what’s the view Controller? 

When you get right down to coming up with a category for an iOS app, it can’t hurt to spot what class it belongs to from the purpose of read of the Apple suggested MVC architecture: model, view or controller? 

  1. Model view Controller (MVC)

could be an idea of building and organizing code that Apple has been actively encouraging us developers to use. If begin with a definition Apple offers, MVC could be a style pattern that assigns objects in associate application one in all 3 roles: model, view or controller. they impart across the abstract boundaries separating one from another. If we elect to use MVC pattern the custom objects are needed to play one in all the MVC roles. In alternative words, if view, model associated controller is concerned in building an app that consists of categories, every of them is allotted the role of a model, a view or a controller. 

  

  1. Why view Controllers aren’t excellent

Developing an app, we tend to treat view Controller as a screen controller. once adding practicality into the app, we regularly add it on to the «screen», that’s a view Controller. Such read ?controller becomes a delegate of tables, pickers, assortment views and alternative objects. However, this simple approach offers birth the subsequent potential problems: 

Duplicating code. This touches upon not solely logic, that ought to be extracted to separate categories, however conjointly boilerplate delegate strategies, that you just have rewrite over and once again. 

Code structure becomes additional advanced just in case of a custom resolution (e.g. uncommon solutions like a pair of table views on one screen). i feel all grasp those difficult constructions within the delegate strategies wont to solely confirm the cell of that table has been designated. 

View Controller becomes an information supply because it stores the information itself and not a model object. 

Code isn’t batched that makes it hard to apportion the code equivalent to one object. 

View Controller stores states. …Not that it’s unfortunate however we tend to may do while not it. 

  

  1. how to handle those drawbacks?

Inherit. If you’ve got ever well-versed Stanford iOS lectures, you would possibly have paid attention to the Core Data View Controller. It encapsulates the code that relates to the information Fetch from the persistent storage, representing it within the UI Table View. Inheritance from this parent category can assist you get eliminate the boilerplate code. however, if you’ve got the UI Collection View rather than the UI Table View, you may get to invent a similar parent category with quite similar logic.

Let’s imagine that we’ve got extracted all the Table View connected code into a separate category and known as if a Table View Data Source. What happens is that View Controller becomes solely answerable for passing our category as a delegate and an information supply of the Table View. Table View Data Source gets got wind of with associate array of the supply information and embellished with blocks. 

error

Enjoy this blog? Please spread the word :)

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Top Mobile and Web App Development Company in USA & India will use the information you provide on this form to be in touch with you and to provide updates and marketing.