Learn with me some conventions, patterns, and three callback mechanisms in iOS (2 ).

Source: Internet
Author: User
Document directory
  • 2. Delegate in iOS, use combination instead of Inheritance

In section 1, we learned about the ObjectC syntax. In section 2, before getting started, we should first understand some basic conventions and patterns in iOS.

 

Foundation. h

The reason why we can easily use ObjectC types such as NSString and NSNumber is that the basic types of C language are encapsulated in the Foundation framework and exposed to us as objects.

So before using it, we need to # import <Fonduation/Foundation. h>. In fact, XCode will do this for us.

 

Various modes in iOS

1. MVC

Let's talk about the MVC pattern that we usually see.
In the textbook definition of MVC, the Model adopts the observer mode, that is, the Model is the observer, and the View is the observer. If the Model changes, the View will receive notifications.
However, in a typical Web environment, the View does not need to be changed in real time. Only when the client sends a request can the View be changed.
In other words, it makes sense to create a View and use the Model only when we need to generate a page as a response and return it to the client.
Therefore, the View does not directly observe the Model, but uses the Controller as the intermediary.

A general flowchart of Asp.net mvc is as follows. It can be seen that the Controller is the entry of the corresponding client request. When a request is sent by the client, the router selects the appropriate Controller for instantiation, then, send the request to the corresponding action for processing.

But in iOS, things have become different,This is becauseView class (usually UIView and Its Class) is responsible for interacting with users. They provide information and accept user events..
The View logic is usually delegated to the Controller for processing, but the View should not directly reference the Controller. In addition to the parent View and child View, they should not reference other views. View can reference the Model class, but generally only reference the specific Model objects they are displaying. For example, a StudentView object may display a Student object.

View accepts events from users but does not process them. When you touch a View, this View may remind a delegate that it has been touched, but it cannot execute logic or modify other views. For example, when a delete key event occurs, only one delegate is prompted to indicate that one delete key has been pressed. View cannot directly delete data from the Model class or delete data from the screen. The Controller is responsible for these functions, that is, the connection between views and models should be established by the Controller.

Controller implements the specific logic of most applications. Most controllers coordinate between the "Model" class and the "View" class. For example,UITableViewControllerCoordinate data models andUITableView. Some controllers coordinate between Model objects and View objects. The names of these controllers are sometimesManagerEnd, suchCALayoutManagerAndCTFontManager. These are examples.

 

2. Delegate in iOS, use combination instead of Inheritance

Configuring objects using delegation is a form of policy mode. Policy mode encapsulates an algorithm and allows you to change the behavior of an object by attaching a different policy (algorithm) object.

A delegate is a policy object that encapsulates an algorithm that determines the behavior of another object. For exampleUITableViewThe delegate of is implemented by an algorithm, which determinesUITableView.

The relationship between View and Controller discussed in MVC can be used to express the relationship between the two. The solid line is direct reference, and the dotted line is indirect reference. Indirect reference can be implemented by id.

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.