Understanding MVC pattern used in iOS apps

30,628

Solution 1

This sample code demonstrates a multi-stage approach to loading and displaying a UITableView. I think it's really interesting to dive in. It will show MVC in the works.

Solution 2

Here’s how the Model-View-Controller (also known as MVC) pattern maps to the main parts of your App:

Model → Data

View → User Interface

Controller → Core Logic

this explain fully with sample code

http://www.hollance.com/2011/04/making-your-classes-talk-to-each-other-part-1/

enter image description here

Solution 3

I believe that the following code will help you understand how to work with MVC in iOS application because its description says:

"MVCNetworking is a sample that shows how to create a network application using the Model-View-Controller design pattern. Specifically, it displays a photo gallery by getting the gallery's XML description, thumbnails and photos from a web server, and uses Core Data to cache this information locally."

http://developer.apple.com/library/ios/#samplecode/MVCNetworking/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010443

Solution 4

  • The model is the brain of the application. It does the calculations and creates a virtual world for itself that can live without the views and controllers. In other words, think of a model as a virtual copy of your application, without a face!

  • A view is the window through which your users interact with your application. It displays what’s inside the model most of the time, but in addition to that, it accepts users’ interactions. Any interaction between the user and your application is sent to a view, which then can be captured by a view controller and sent to the model.

  • Controllers in iOS programming usually refer to view controllers. Think of view controllers as a bridge between the model and your views. They interpret what is happening on one side (what the user does on the view side, or the information provided by the model) and use that information to alter the other side as needed.

Solution 5

This is by far the best, yet simple explanation I've come across(taken from RayWenderlich)

"The idea behind MVC is that
- VIEWS should only care about how they are presented HOW THEY ARE PRESENTED,
- MODELS should only care about their DATA,
- and CONTROLLERS should work to MARRY the two WITHOUT necessarily knowing too much about their internal structure."

Share:
30,628

Related videos on Youtube

TheLearner
Author by

TheLearner

Updated on July 09, 2022

Comments

  • TheLearner
    TheLearner almost 2 years

    I have read Apple's MVC article and am confused about various things. Firstly Apple uses a combination of the View and the Controller in almost all its sample applications which is fine and I like it but they contradict themselves in this article because they said that View's should not rely on Controllers etc.

    My main question is does anyone have a link to one of Apple's sample iOS projects which is a good example of the MVC pattern - with data retrieval etc because I don't fully understand the Model part of the pattern.

    I don't understand the difference between a 'domain object' and a model object. For example if I wanted to retrieve a list of orders this would happen in a model class Orders. Would I then have another class Order which has properties such as OrderDate, OrderNumber etc or how would this work?

    • Black Frog
      Black Frog about 13 years
      not all Apple sample code follows proper MVC. The sample code is to demo/show an API. Also the model object in these sample can be as simple as NSMutableDictionary object.
  • trillions
    trillions about 11 years
    The part I am confused on is where to put Http web service calls. In the past, when I worked on MS MVC, we put web service calls in controllers. But for ios development, i got suggestions on adding http web service calls to Model. I feel either way works, but I am not sure which way is more of a good MVC pattern..Do you have any insight on this? thanks!
  • Biclops
    Biclops almost 11 years
    I'm the same way, I keep them separate. Though I've seen implementations where service calls are in the model that work quite well. The example that comes to mine is for a uiimage, where service call is embedded into that class so that it retrieves an image from he web while showing a placeholder image. But typically I see code structure like that in this sample developer.apple.com/library/ios/#samplecode/LazyTableImages/‌​…
  • Fattie
    Fattie about 10 years
    "My main question is does anyone have a link to one of Apple's sample iOS projects which is a good example of the MVC pattern"
  • codercat
    codercat about 10 years
    the answer only code. that is not describes about MVC fully
  • vikingosegundo
    vikingosegundo about 10 years
    I hope that the OP got MVC right since he posted the question 3 years ago.
  • codercat
    codercat about 10 years
    but it is helpful for newbie developer folks
  • Primoz990
    Primoz990 about 8 years
    The first link is broken

Related