MVC for IOS

Source: Internet
Author: User

The MVC pattern was first proposed by Trygve Reenskaug in 1978 [1]. It was a software design pattern invented by Xerox PARC in 1980s as the program language Smalltalk. The purpose of the MVC pattern is to implement a dynamic program design, simplify subsequent modifications and extensions of the program, and make reuse of a part of the program possible. In addition, this mode simplifies the complexity to make the program structure more intuitive. The software system also gives the functions of each basic part by separating its basic parts. --- (Wikipedia)

MVC has been widely used. MFC, J2EE, Java Swing,. NET Perl Ruby on Rails, Python, JavaScript, PHP, ActionScript3, etc. MVC is also used for IOS development. The following is a brief introduction to MVC development in IOS.

 

Model-reflects what your program is, but it doesn't care how it is displayed

Controller-how your model is presented to the user. It cares about the UI Logic

View-render data, present it to users, and receive user feedback.

 

All the content of MVC is the division of labor and communication between the above three.

 

1) the Controller can communicate with model and view directly.

Generally, the controller directly references the model and view instance object.

 

2) Model and view should not communicate directly

 

3) communication between views and controllers

A) View receives some user events, but does not involve business processing. Therefore, the user event is generally returned to the Controller through the protocol-delegate method.

 

B) A View cannot have data, but a view needs data to be displayed. It is generally obtained through the Protocol. For example, UITableView obtains data by defining the UITableViewDatasource protocol.

 

 

4) Controller is generally used as a data source (rather than a model ).

Similarly, refer to UITableViewController, which implements UITableViewDataSource instead of model.

 

5) when the Controller needs to parse/format the data returned by the model to the View

View does not focus on business logic. The Model does not focus on the view. This causes the model to return data, which may not be suitable for direct display of views in some cases. Therefore, the controller usually needs to process the data for the view before handing it over to the view.

Another typical case is the handling of system error messages. System error messages are varied, but not every piece of information is suitable for direct display to users. Therefore, the controller must first process the error information before releasing it and handing it over to the view.

 

 

6) How does Model communicate with controller?

The Model should be independent from the UI. If the model has information to be updated or any other event needs feedback, the message/notification mode is generally used.

For example, some background processing programs: email refresh, data statistics, etc... The user is not concerned (No UI trigger), but needs to prompt the user. At this time, you can set up a message receiver in the corresponding controller, and the model will issue the information through notification when the response needs to be triggered. In fact, it does not care about the subsequent situations of messages.

 

 

The following is a diagram of MVC in the Stanford tutorial. It is concise and clear.

 

Related Article

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.