MVC Question

Discussion in 'iOS Programming' started by blue-lion, Mar 15, 2012.

  1. blue-lion macrumors member

    Joined:
    Jan 26, 2010
    #1
    Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3)

    Hi. I have a question regarding the MVC design concept. In most diagrams I have seen ( including material from the stamford lectures) there is no allowance for data to be passed back from the model to the controller. And yet data has to be passed back in order to be passed to  a view,right?. Or maybe I'm mistaking data for communication? 

    I have a private model object declared in my main controller as per recommended in the lectures ,but surely  the results have to be fed back from the model to the view via the controller?
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    In the first lecture of 2010 for the Stanford class, there was a slide showing how the data travels.

    As I recall, KVO (Key-Value-Observing) is used to alert the Controller of changes in the Model, and then the Controller tells the View directly.

    Then again, I suck at grasping MVC and have to ask for help regarding it all the time.
     
  3. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #3
    The way I do it is simple. The Controller has instances of both my Model and my View classes and is responsible to be a broker of sorts between the two. The View and Model communicate back with the Controller on changes or requests for information using Objective-C protocols, the Controller being the delegate.
     
  4. chown33, Mar 15, 2012
    Last edited: Mar 15, 2012

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    The Controller is not always a middleman.
    http://en.wikipedia.org/wiki/Model–view–controller#Concepts
    The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). [underline added]​
    The View has a direct connection to the Model (see diagram at top of that article), so it clearly has direct access to any data objects exposed by the Model. If it's unclear, the "information about its state" means "data". There is no need for the Controller to sit between these two for this purpose.

    Also see item #4 under the Overview heading of that article.

    So yes, I think you are mistaking data for communication or association.

    EDIT
    An example of a Model object that supplies data directly to a View is UITableViewDataSource. The data-source is the Model. Or more specifically, the Model object conforms to the UITableViewDataSource protocol. There is no controller object sitting between the UITableView and its data source. There is a controller involved in the overall interaction, but it's not sitting in the data-path between the Model and View.
     

Share This Page