IOS Event Responder Chain learning (also known as UI chain chaining)

Source: Internet
Author: User

When an event response occurs, you must know who responds to the event. In iOS, the response chain responds to events, all of the event response classes are subclasses that inherit from Uiresponder, and the response chain is a hierarchy of different objects in which each object gets the chance to respond to the event message in turn

after a touch event occurs, the system takes the event to an event managed by UIApplication Queue

Benefits : Following the first response principle, priority is always given to the top events in the queue . (Stack is advanced post-out does not conform to business logic)

(1) Event delivery (chain)

1. There is no controller in the delivery chain, because the controller itself does not have the concept of size

2. The user's touch event will be intercepted by the system first, and then packaged and processed. Then recursively traverse all of the view, Touch Test (hitTest), until you find the view that can handle the event

Approximate process application–> window–> root view–>......–>lowest view

Online a very intuitive explanation diagram

(2) Response chain

1. there is a controller in the response chain because the controller inherits from the Uiresponder

2. When a view is able to handle a touch event, start responding to the event.

The system calls the following methods of view:

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event;

-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event;

-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event;

-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event;

Multiple objects can respond to events together. You only need to call the Super method in the above method overloads.

Approximate process initial view–> Super view–> .....–> view controller–> window–> Application

I remember being interviewed by a CTO the year before last asked this question, the exact words " trigger a button, how to intercept its trigger message ", at that time, but because of a creative problem analysis to answer particularly in place, also praised me, said I have an idea, he still cold, He said he was going to blow his nose and not come back. The more regrettable interview, actually do not estimate I am a girl, I am not afraid to refuse, also did not say why, there is no then.

Now think of it, estimating the principle of "event delivery responder chain" is really important, now learning more deeply, will naturally discuss learning, in the development of the application. Really grow like a shed.

Example:

In fact, in the development, really a lot of places can be applied to this method, used to intercept some information, processing messages, get objects, modify the response, or specifically know how to implement a method in the end is how a process and so on.

Requirements: Add a button to the cell and click to go to the next navigation page.

The usual practice is that "[Cell.btn Addtarget:blabla]" is written in the view controller because it is a good call to Viewcontroller.

Now that we know the principle of the event responder chain, use this mechanism to solve it.

2.1 First create a uiview category extension An instance method is based on the " event responder Chain principle ": reads the click event on the UIView, and if it has been passed to Uiviewcontroller, it is processed. Gets the view controller.

////uiview+eventlink.h//Tableviewdemo////Created by HF on 16/2/26.//copyright©2016 year Hf-zhjh. All rights reserved.//#Import<UIKit/UIKit.h>@InterfaceUIView (Eventlink)-(Uiviewcontroller *) Viewcontroller; @end////UIVIEW+EVENTLINK.M//Tableviewdemo////Created by HF on 16/2/26.//copyright©2016 year Hf-zhjh. All rights reserved.//#Import"Uiview+eventlink.h"@implementation UIView (eventlink)//extend a method for UIView to respond to event chains-(Uiviewcontroller *) viewcontroller{Uiresponder*nexres=[self nextresponder];  Do {        //to interpret whether the current responder is Uiviewcontroller        if([Nexres Iskindofclass:[uiviewcontrollerclass]]) {            //whether to directly handle            return(uiviewcontroller*) Nexres; }Else{            //or keep looking .nexres=[Nexres Nextresponder]; }    }  while(nexres!=nil); returnNil;} @end

By extending a taxonomy (category) method for UIView, we can call the classification method (Viewcontroller) in the cell to handle the event response.

// Button Events
UITableViewCell inherited from UIView
It's actually very convenient and very readable.
-(void) Buttonaction: (UIButton *) btn{ // use event responder Uiviewcontroller * view=[[Uiviewcontroller alloc] init]; [self.viewcontroller. Navigationcontroller Pushviewcontroller:view Animated:yes]; [View release];}

(3) Several cases of unacceptable touch events

1. Do not receive user interaction

userInteractionEnabled = NO

2. Hide
hidden = YES

3. Transparent
alpha = 0.0 ~ 0.01

Tip: Uiimageview's userinteractionenabled default is no, so Uiimageview and its child controls cannot receive touch events by default.

IOS Event Responder Chain learning (also known as UI chain chaining)

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.