Ios-responder Chain

Source: Internet
Author: User

In iOS, when an event response occurs, you must know who is responding to the event. This is the response of the responder chain to the event, all the event response classes are Uiresponder subclasses, the responder chain is a hierarchy of different objects, each of which will sequentially get the opportunity to respond to event messages. When an event occurs, the event is first sent to the first responder, and the first responder is often the view where the event occurs, where the user touches the screen. The event is passed down the responder chain until it is accepted and processed.

In general, the first responder is a view object or its subclass object, when the event is left to him to handle after the touch, and if he does not handle it, the event is passed to its View controller object Viewcontroller (if present), then his parent view (Superview) object (if present) , and so on until the top-level view. Next, you will follow the top view to the window (UIWindow object), and then to the program (UIApplication object). If the entire process does not respond to this event, the event is discarded. In general, events stop passing as long as there is an object handling event in the response chain.

A typical response route is as follows:

First responder-->the window-->the Application-->app Delegate.

With the above basics, let's talk about events. The events in iOS are grouped into three categories, touching the screen, shaking the device, and remotely controlling the device. The following three types of events are available:

(1) Touch screen time (touch Event);

(2) Sports events (motion event);

(3) Remote control events (Remote-control event);

Now let's take a touch event as an example to get a closer look at the responder chain (Responder Chain).

Responder objects (Responder object), which refers to objects that have the ability to respond and handle events. A responder chain is a hierarchical structure of a series of responder objects. Uiresponder is the base class for all response objects, and the interfaces that handle each of these events are defined in the Uiresponder class. The familiar Uiapplication,uiviewcontroller,uiwindow and all Uikit classes that inherit from UIView are directly or indirectly inherited from Uiresponder, so their instances are the responder objects that can form the responder chain. As shown in the following:

As you can see, the responder chain has the following characteristics:

(1) The responder chain is usually composed of a view (UIView);

(2) The next responder of a view is his view controller (Uiviewcontroller), if any, and then passes it to its parent view (Superview).

(3) The View controller (if any) the next responder is the parent view of the view that it manages.

(4) The content view of the Singleton window (UIWindow) will point to the window itself as its next responder.

(5) The application of a single case (uiapplication) is the end point of a responder. Its next responder points to nil to end the entire loop.

Then learn about the next event distribution (Delivery):

First Responder refers to the current responder object (usually a UIView object) that accepts the touch, meaning that the object is currently interacting with the user, which is the beginning of the responder chain. The mission of the entire responder chain and event distribution is to find the first responder. The UIWindow object sends the event as a message to the first responder, giving it an opportunity to handle the event first. If the first responder is not processed, the system passes the event (via message) to the next responder in the response chain to see if he can handle it.

Now I'll use a few words to briefly explain what the responder chain is:

(1) The responder chain is a queue that is linked by multiple responder objects;

(2) The responder object refers to the object that can handle the event, that is, the object that inherits from Uiresponder;

(3) The responder chain enables multiple objects on a chain to respond to the same event;

(4) The message is passed on the responder chain, which is the object that wants to find the next response (Nextresponder);

Here we have to notice a problem, for the management of message events, we use the queue, not the stack, because we want to ensure that the first occurrence of the event first processing, that is, FIFO.

Ios-responder Chain

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.