This article introduces the MVC design pattern and its usage skills in iOS development.
MVC is the origin of design patterns and one of the most used design patterns in the field of software engineering.
MVC is: Model models, view views, controller controllers
Model:
Encapsulate data in model objects and define specific logical behavior for manipulating data
Models can represent complex data structures, which can be data displayed on the screen, data waiting to be processed, etc.
View:
Using view objects to present information to users
The view object responds to the user's actions and knows how to show itself on the screen
A view object typically obtains data from the application's model object for presentation.
You can work with part, whole, or multiple model objects of a model object.
Controller:
Use a controller to associate a model with a view, just like an intermediary between a view object and a model object.
The Controller object can also perform other actions for the application, such as managing the life cycle of other objects, setting up and coordinating work
The user passes the value obtained from the View object, such as in a text box, to the Controller object, and the Controller object allows the view object to change its appearance or behavior based on this user action.
1. Encapsulating model Objects (arrays) in the controller
If it is a simple object (such as NSString), you do not need to encapsulate it with a model class
If the non-OC object, it should be a certain encapsulation;
If it is a dictionary data, it should be a dictionary to model
2. Dictionary Turn model
The source of data in iOS is often: JSON, plist, XML, and so on, which are first converted to dictionaries or arrays.
To facilitate the management of the data in the dictionary, a dictionary-to-model approach is used to manage the model objects.
The dictionary key as the property name in the model
Value in the dictionary is used as the property values in the Model object.
In a method that constructs a model object, the pass-through dictionary initializes the property,
3. Model Array properties of the controller should be lazy loading mode
That is: Rewrite the getter method of the array so that the model data is loaded the first time it is used.
When the property pointer is nil, the data is loaded from the specified location in the program, and an array is created to store the data in the array.
The specified location can be a local database, plist, JSON, data passed over by the network, and so on.
Pure Code Design view:
1. Define a sub-class of views
2. Provide a class method to create a View object
Class 2.1 Method Encapsulation Alloc+init
2.2 Overriding the Init method, creating all sub-views and making one-time property settings
2.3 Screen fit: Override the Layoutsubviews method or use AutoLayout
3. View subclasses Add data Model properties and override setter methods to display data to a child view
Data model properties may not be required if the child view's data is pinned
Xib Design view:
1. Design a view in Xib, design the child controls and their related properties in the view, and make a screen fit
2. Define a subclass of a view, the parent class type should be the type of the view class set in Xib, and be associated in Xib
3. Provides a class method for creating a View object, using Xib to load the View object
3. View subclasses Add data Model properties and override setter methods to display data to a child view
Data model properties may not be required if the child view's data is pinned
The controller acts as a bridge between the view and the model, and the answer has the following functions:
1. Managing View Objects
2. Managing model objects (arrays)
3. Implement the associated business logic, such as: Creation of child views, assignment of models, etc.
4. Handle various complex business logic, such as: Response action when button is clicked
The controller can be divided into the following categories:
View Controller: As a manager of a page
Agent controller: The data source and proxy methods used to implement some objects
Model controller: Manage complex model data, including acquisition, parsing and other functions
...
This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1746499
MVC of iOS Development design patterns