Model objects
Model objects encapsulate the application's data and define the logic and operations that manipulate and manipulate the data. For example, a model object might represent a role in a game or a contact in an address book. The actions that the user creates or modifies data in the view layer are communicated through the Controller object and eventually the model object is created or updated. When a model object changes (for example, new data is received over a network connection), it notifies the controller object that the Controller object updates the corresponding View object.
View Object
A View object is an object that the user can see in the application. The View object knows how to draw itself and may respond to the user's actions. The primary purpose of a View object is to display data from an application model object and make that data editable. However, in an MVC application, the View object is usually detached from the model object.
In iOS application development, all controls, windows, and so on are inherited from UIView, corresponding to the V in MVC. UIView and its subclasses are responsible for the implementation of the UI, and the events generated by UIView can be entrusted to Uiviewcontroller.
Controller Object
A Controller object acts as a medium between one or more view objects and one or more model objects in an application. The Controller object is therefore a synchronous pipe program through which the view object understands changes to the model object, and vice versa. The Controller object can also perform settings and reconcile tasks for the application and manage the life cycle of other objects.
The Controller object interprets user actions in the View object and communicates new or changed data to the model object. When a model object changes, a controller object communicates the new model data to the View object so that the view object can display it.
1, model and view can never communicate with each other, can only be passed through the controller.
2, controller can directly with the model dialogue (read and write call model), model through the notification and KVO mechanism and the controller to communicate indirectly.
3, controller can directly with the view dialog (through outlet, direct operation View,outlet directly to the control in the view), view through action to the controller to report the occurrence of events (such as the user Touch me). The controller is the direct data source of the view (the data is most likely the controller obtained from model). Controller is the proxy for view (delegate), in order to synchronize view with Controller,delegate is a set of protocols, indicating that the program will or is already in a certain state, to adjust the view to the user has an explanation. For example, if the system is running out of memory, you are not reducing the quality of the view to save memory.
Note: Suddenly out of a delegate, it is difficult to understand, in fact, he does not correspond to the Xxappdelegate file that Xcode created for us, this file is not part of MVC, although it is associated with MVC.
The first part said so much finally humorous, how to use the MVC model better to lean to VC, improve reusability and maintainability it? I drew the following diagram:
VC holds the view and model part, view through the proxy or target-action way the user's actions to VC,VC responsible for different user behavior to make different responses. If you need to load or refresh the data, call the model exposed interface directly, and if the data can be synchronized, refresh the view directly using the data obtained. If the data needs to be obtained through other asynchronous means, such as network requests, the VC will listen to the data update (success or failure) notification by the model, and when the notification is received, the view is refreshed according to the success or failure. It can be seen that the whole process of view and model is not directly interactive, all operations are through the VC coordination.
Understanding the MVC design pattern in iOS for iOS development