IOS MVC Architecture (design pattern) and MVVM

Time:2021-12-31

This article is reproduced from:Star Moon 0

MVC(Model-View-Controller)

IOS applications follow MVC architecture, except swiftui launched by IOS 13. The design of swiftui follows MVVM
MVC is the standard model for building IOS apps. It is an authoritative paradigm recommended by apple to organize code. Most apps on the market are built in this way

  • Model: data (business) model, which is responsible for processing and accessing data

  • The model may not be an OC object at all, it may be a network API The database is updated, and the model has to inform the controller

  • View: interface, which is responsible for displaying data and interacting with users

  • Controller: business logic, equivalent to a processing center, is responsible for managing model and view. Model and view are objects in the controller. They are mainly responsible for displaying the data model on the view and handling interface interaction.

  • Model and view are not interlinked. Everything can only be exchanged through the controller

  • The task of the controller is to show the model and take charge of the model.

  • The controller translates and formats the model data they need for the views

  • Controlling how the UI is displayed is what it does every day

As a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my IOS communication group:711315161, whether you are Xiaobai or Daniel, welcome to settle in, share bat, Ali interview questions, interview experience, discuss technology, and exchange, learn and grow together!

It is easy for the controller to communicate with the model or view, because the controller itself holds the model and view. The model and view objects are created in the controller. All controllers can easily obtain the properties and methods of the model and view objects

To communicate with the controller, the view can add target action events, delegate and datasource delegate through the button

The model shall communicate with the controller through KVO and notification

Will get a lot of MVCs, including layers and assisting each other
IOS has an API that allows a controller to view other MVCs as its own, as shown below

As shown in the figure below, this can’t work. It will make you dizzy, so make a red circle and eliminate it

MVVM(Model-View-ViewModel)

Although MVC is the standard mode of IOS app in the storyboard project, it also has a nicknameMassive view controllerThat is, when the business logic is complex, the viewcontroller will print a lot of code. In order to downsize the controller, the ViewModel layer is introduced to handle network requests, data conversion and other business logic (which is also done by the controller in MVC). The controller is more responsible for the functions related to interface display and user interaction, and combines the controller and view into view
In this way, MVVM architecture is derived from MVC.

  • Model: Data
  • View: view display (uiview + uiviewcontroller)
  • ViewModel: connect view and model

swiftUI(MVVM)

The swiftui launched by ios13 Apple‘s standard architecture is MVVM. We don’t need to be like when using storyboard. Apple’s MVC was originally Apple’s standard mode. We just wanted to dismantle an MVVM

Recommended Today

Could not get a resource from the pool when the springboot project starts redis; nested exception is io. lettuce. core.

resolvent: Find your redis installation path: Start redis server Exe After successful startup: Restart project resolution. ———————————————————————->Here’s the point:<——————————————————————- Here, if you close the redis command window, the project console will report an error. If you restart the project, the same error will be reported at the beginning, The reason is: It is inconvenient to […]