iOS Development Beginner Tutorial 2 - MVC in iOS Development

MVC is a strategy how to organise all the classes in your application. All the classes in a app has 3 camps. They are, 
  1. Model - Model is what is your program. Model is UI independent.
  2. Controller - How something draw in your screen is the Controller’s job.
  3. View - View is the building block of the UI. Stuff in your view is generic
To know MVC correct you have to study how those 3 elements communicate with each other. 
Let’s talk about Controller talks to the Model. Controller can directly talks with Model. In the above picture also you can see it clearly. It’s a full unrestricted access. As well as Controller also can talk directly to their View. It layout the user interface to the View to draw in the screen. You can see, there is a name called “Outlet” in the start of that arrow. When we have a property of a controller that point in to the View we call it Outlet.The Model and View should never speak to each other. Model is 100% UI independent. There is no way Model can talk to View. View want to go through Controller to talk to the Model.

View can communicate with the Controller in a blind way. They don’t know the class they talking to. It’s a structured way. Controller has a target & the View has a action. That action can only go the that target. The View sends the action when things happen in the UI. When the View has some complicated ways to communicate. The Controller sets itself as the View’s delegate on those occasions. In the above picture with the Yellow arrow you can see a delegate. Delegation also a blind communication way. And also Views do not own the data they display. When you see something in your screen, it’s not the View’s data. The data is owned by the Model & Controller give those data to the View from Model. To get those data from the Model, View has to use Data Source. Data Source is just a kind of delegate, it’s a specific delegate. In building iOS apps you have to use delegates & also data sources.

Can the Model communicate to the Controller? Good questions. But when we are building apps we have some situations that Model want to communicate to Controller. For example think about a online radio streaming app. When the stream gives data to the Model, Model wants to communicate with the Controller & want to send those data to the Controller. How do we do that communication? It’s similar to radio station broadcast. The mechanism for doing this in iOS is called Notification & KVO. So the model changes, it broadcast that change & Controller tune in to interesting stuff.



This is how MVC works in iOS app development. Hope this will be helpful for your knowledge.


Dilum BC Navanjana
[CIS 2010/2011]
Associate Software Engineer(iOS), 
Creation Application
dilum@creationapplication.com

0 comments :