An explanation of MVC and MVVM patterns in iOS development

Source: Internet
Author: User

MVC in iOS (Model-view-controller) The software system into Model, View, Controller three parts


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, controlled by the controller
the controller can access the model directly, or it can 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, when the button is clicked, View will pass 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, shouldThe controller will also be notified when did change is received. 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:
        Let Center = Nsnotificationcenter . Defaultcenter ()

Center.
Addobserverforname ( uicontentsizecategorydidchangenotification ,
Object:
uiapplication . sharedapplication (),
Queue:
Nsoperationqueue . Mainqueue ())
{Notification
inch
Let c = notification. UserInfo ? [ Uicontentsizecategorynewvaluekey ]         }
uicontentsizecategorydidchangenotification and Uicontentsizecategorynewvaluekey are all defined string in the system
KVO:
in the Viewdidload: WebView . Addobserver ( Self , Forkeypath: "Estimatedprogress" , Options:. New, Context: Nil )
Override func Observevalueforkeypath (keypath: String ?, Ofobject object: Anyobject ?, change: [ NSObject : Anyobject ]?, Context: Unsafemutablepointer < Void >) {               if (KeyPath = = "Estimatedprogress" ) {                   Progressview . Hidden = WebView . estimatedprogress == 1                   Progressview . setprogress ( Float ( WebView . estimatedprogress ), Animated: true )              }          }
MVP Modeevolved from the classic MVC model, the controller is replaced with presenter, according to the MVP Baidu Encyclopedia, the advantages of MVP compared to 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 communicate directly have different claims, but in the development of iOS,That 's what Apple says. 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.
the correct practice with the MVVM pattern is that we should create a viewmodel for the root view of the app delegate, and when we want to generate or show another secondary Viewcontroller, Use the current ViewModel to create a sub-viewmodel for it.
While these viewmodel codes are not in Viewcontroller, our view requests its own viewmodel to get the data it needs, and Viewcontroller is completely attributed to view.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

An explanation of MVC, MVVM patterns in iOS development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.