MVC in iphone apps
can somebody explain me the basics of mvc in ipho开发者_如何学Pythonne apps?
I see the template classes, but how do they communicate?
does UIView send events to the Controller? or does the model send events?
what of the template files is the model?
I would strongly recommend the iTunesU, Stanford University series titled "Developing Apps for iOS (HD)" by Paul Hegarty.
Lesson 1 discusses MVC and lesson 2 demonstrates it.
These lessons are free, and well worth a view if you have the time.
The model shouldn't be concerned with the view. The same holds true for the view, it should be decoupled from the model and not really have direct access to it, despite the fact it will be displaying information from the model for the user to see and interact with. The controller acts as intermediary between models and views.
In Apple's words, "A controller object interprets user actions made in view objects and communicates new or changed data to the model layer. When model objects change, a controller object communicates that new model data to the view objects so that they can display it."
From Jeff Atwood's Excellent blog "Coding Horrors":
Like everything else in software engineering, it seems, the concept of Model-View-Controller was originally invented by Smalltalk programmers.
More specifically, it was invented by one Smalltalk programmer, Trygve Reenskaug. Trygve maintains a page that explains the history of MVC in his own words. He arrives at these definitions in a paper he published on December 10th, 1979:
Models Models represent knowledge. A model could be a single object (rather uninteresting), or it could be some structure of objects.
There should be a one-to-one correspondence between the model and its parts on the one hand, and the represented world as perceived by the owner of the model on the other hand.
Views A view is a (visual) representation of its model. It would ordinarily highlight certain attributes of the model and suppress others. It is thus acting as a presentation filter.
A view is attached to its model (or model part) and gets the data necessary for the presentation from the model by asking questions. It may also update the model by sending appropriate messages. All these questions and messages have to be in the terminology of the model, the view will therefore have to know the semantics of the attributes of the model it represents.
Controllers A controller is the link between a user and the system. It provides the user with input by arranging for relevant views to present themselves in appropriate places on the screen. It provides means for user output by presenting the user with menus or other means of giving commands and data. The controller receives such user output, translates it into the appropriate messages and pass these messages on to one or more of the views.
The best way to understand this (like many core concepts of iOS programming cough HIG cough), is to read Apple's documentation and try it. Here is a good design pattern overview (including MVC) for beginners, including a tutorial: Link
精彩评论