Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC in Cocoa Touch: How do the view and the model interact?

I’ve always thought I understood MVC, but lately, after reading a lot of Stack Overflow posts on the subject, I’ve discovered that the ways in which MVC frameworks do things are slightly different from one another. More specifically, in the way in which the view and model interact, there seems to be two schools of thought:

  1. When the user interacts with the view, the view notifies the controller, and the controller in turn does something to the model. When the model changes, the model notifies the controller, which in turn updates the view.

  2. The view subscribes to the model. When the model changes, the view seems to be notified directly that it needs to update itself.

enter image description here

So my question is: In Cocoa Touch (iOS), what is the best way to do MVC? I’m mainly coding for iOS these days and am interested in the best practice for this platform only. (I’m not interested in how ASP.NET, Rails, Backbone, etc. do things.)

It would be wonderful if some KVO example code could be provided. Thanks. =)

like image 943
Louis Nguyen Avatar asked Feb 10 '12 19:02

Louis Nguyen


People also ask

How does model interact with view?

The view means presentation of the model in a particular format. The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model.

What does model do in MVC?

The Model is the part of MVC which implements the domain logic. In simple terms, this logic is used to handle the data passed between the database and the user interface (UI). The Model is known as domain object or domain entity. The domain objects are stored under the Models folder in ASP.NET.

Can you explain MVC and how it's used on Apple's platforms?

MVC – short for Model-View-Controller – is Apple's preferred way of architecting apps for its platforms, and so it's the default approach used by most developers on Apple platforms. In MVC each piece of your code is one of three things: Models store your data, such as the names of products in a store.

What is the Model View Controller design pattern?

The model-view-controller (MVC) design pattern specifies that an application consist of a data model, presentation information, and control information. The pattern requires that each of these be separated into different objects.


2 Answers

Apple advocates use of the first method, I believe.

It is the modified version of the standard MVC model (the second approach), where model and view are totally separated. Personally I think that it’s cleaner and more extensible.

  1. The logic is centralized in the controller.
  2. There is no need to write custom views to handle events from the model. Normally you’d write a custom controller but use the view classes given by the SDK. Following the second method, you might have to create a custom view just to handle events from the model.
like image 160
hdoan Avatar answered Sep 23 '22 20:09

hdoan


I found that the best basic theory on the matter is taught in Stanford University by a very talented teacher named Paul Haggarty. I recommend looking this course up on iTunes U - there are 18 lectures in HD video and ppt files to learn from. Here is a link to the course website: http://www.stanford.edu/class/cs193p/cgi-bin/drupal/

I remember that he goes through the MVC part of the material very quickly but thoroughly, making this matter very clear. Also, I would have to say that the #1 school of thought is the one I agree more with.

like image 34
Stavash Avatar answered Sep 23 '22 20:09

Stavash