The application of M-V-VM in the development of iOS

Source: Internet
Author: User

Item Development, often encountered an interface is very complex, various control various View, with each other complex interactive logic, the data of each component sometimes need specialized server interface support .

The MVC pattern puts all this logic into C , making a viewcontroller very large and complex. Whether it's a change or debug, it's time consuming.

This can be improved by introducing the M-V-VM pattern into MVC . The structure is as follows:

explain :

       viewmodel< Span style= "font-family: Song body, SimSun; font-size:14px; " > viewcontroller Create and manage these viewmodel

ViewModel manages refresh and user action events for View. If the user's response requires other components to interact, pass the delegate up to Viewcontroller. Communication routing between ViewModel is performed by Viewcontroller .

View provides an API for users to refresh the display, and the concrete refresh of inner elements is not visible externally.

In addition to refreshing the display, View also provides a response to user actions, which is handled by delegate notification ViewModel .

Datamodel is managed by ViewModel . It has the ability to get the data of the View, to save the data to a structure. A common property is typically created to hold the new data obtained. ViewModel establishes KVO monitoring for this attribute .

We typically use asynchronous methods to download new data from the network, and when the new data is downloaded successfully, it is parsed and stored in public properties. At this point,the KVO method is activated, and the value of the ViewModel read property is passed to the View, which refreshes the display .

Datamodel provides an update data method similar to Reloaddata, called by ViewModel . ViewModel As long as this method is called, everything starts to run automatically .

Datamodel is only visible to ViewModel . In this way, the three have formed a relatively independent ecological circle, the outside can only see ViewModel. ViewModel provides external APIssuch as refreshing data, submitting cross- ViewModel communication requests, and more .

Demo Project is here

The application of M-V-VM in the development of iOS

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.