First, the concept of MVC
The full name of MVC is the Model View controller, which is the abbreviation for Models-View-controller, a software design paradigm. The purpose of MVC is to separate the implementation code for M and v so that the same program can use different representations. C exists to ensure the synchronization of M and V, and once M is changed, V should be updated synchronously.
Ii. Inter-MVC communication
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). View can communicate with the controller via delegate.
Below, based on the MVC of iOS, add:
1. In the illustration, the green arrow indicates a direct reference. The direct reference to view is reflected in the Iboutlet. When referencing a view, such as a button. Need to be in Viewcontroller
The declaration of a Iboutlet UIButton * BTN;
2. Then, let's see how the view communicates to the Controller. For this, IOS has three common patterns:
Sets the action Target for the view. If you set UIButton's touch up inside action Target.
Set the delegate of the view, such as uialertviewdelegate, uiactionsheetdelegate,uitextfielddelegate, etc.
Set the data source for the view, such as Uitableviewdatasource.
With the above three modes, view can communicate with the controller without needing to know who the controller is, so the view is decoupled from the controller.
MVC Design Patterns in iOS