The MVC (Model-view-controller) in iOS divides the software system into Model, View, Controller three parts, the structure diagram is as follows:
Model: What your app is essentially (but not how it's presented) controller: How your model is presented to the user (UI logic) View: The user sees that the controller manipulated by the controller can access the model directly, You can also control the view directly. But model and view cannot communicate with each other. View can access the controller in a action-target way, such as when we drag UIButton to the @ibaction created in the storyboard, and when the button is clicked, the view passes that information to the controller. Sometimes the controller needs to monitor the state of the view in real time, and the controller will set itself as the delegate of the view via protocol, so that when view will change, should Controller will be notified accordingly. View does not store data, but view can use the protocol to obtain a controller instead of the data in the model for presentation. The controller collates the data in the model for display to the view. Model cannot communicate directly with the controller because the model is independent of the UI. However, the broadcast mechanism can be used when the model changes to notify the controller, with Nsnotification and KVO (Key-value observing) available in iOS. Nsnotification:
1 Let center = nsnotificationcenter.defaultcenter ()2 3 Center.addobserverforname (uicontentsizecategorydidchangenotification,4 Object : Uiapplication.sharedapplication (), 5 queue:NSOperationQueue.mainQueue ()) 6 inch 7 Let C = Notification.userinfo? [Uicontentsizecategorynewvaluekey] 8 }
KVO:
1Webview.addobserver (self, Forkeypath:"estimatedprogress", Options:. New, Context:nil)2 3 OverrideFunc Observevalueforkeypath (keypath:string, OfobjectObject: Anyobject?, change: [Nsobject:anyobject]?, context:unsafemutablepointer<void>) {4 if(KeyPath = ="estimatedprogress") {5Progressview.hidden = Webview.estimatedprogress = =16Progressview.setprogress (Float (webview.estimatedprogress), animated:true)7 }8}The MVP model evolved from the classic MVC model, replacing the controller with presenter, according to the MVP Baidu Encyclopedia explanation, the MVP advantage compared to the MVC is completely separate model and view, Model and view information transfer only through Controller/presenter, I look at the data found in other platforms MVC mode view and model can directly communicate there are different statements, but in iOS development, Apple said so. Under MVC, all objects are categorized as a model, a view, or a controller. The model holds the data, the view shows the interface to interact with the user, and the view controller mediates the interaction between the model and the view, and in iOS development I understand that the model and view cannot communicate with each other. MVVM (Model View View-model) shows the difference between MVVM and MVC. In the MVC model of iOS development, controller assumes too much code, including our view processing logic and business logic. In MVVM, we stripped the view processing logic from C to V, and the rest of the business logic was called View-model. iOS apps that use the MVVM pattern are better testable than MVC, because ViewModel does not include updates to the view, reducing the burden on the controller compared to MVC, making it more reasonable to divide the functions.
A concise introduction to the MVC and MVVM patterns of iOS