iOS response event delivery, Nextresponder research

Source: Internet
Author: User

Here, we consider the following two scenarios.

Question 1. How do I invoke the method inside the controller of the parent view?

The answers are as follows:
[[Self Superview].nextresponder method];
[[Self Superview] nextresponder] method];
[Self.nextresponder Method];
Above all can, see the situation use, when using the best judge.

Official explanation
UIView implements this method by returning the Uiviewcontroller object, the manages it (if it has one) or its superview (i F it doesn ' t); Uiviewcontroller implements the method by returning its view ' s superview; UIWindow returns the Application object, and UIApplication returns nil.

This can be judged by the following code:

ID next = [self nextresponder];
while (![ Next Iskindofclass:[viewcontroller class])//Can't jump out here ... Some people say here can not jump out, in fact, because it does not have the current view into the Viewcontroller, nature will not jump out, will die cycle, use needs attention.
{
Next = [Next Nextresponder];
}
if ([Next Iskindofclass:[viewcontroller class]])
{
Controller = (Viewcontroller *) next;
}


Question 2: When a sub-view needs to receive click events, and the parent view also needs to receive click events, how to do?

Of course, you might say that it is possible to call Mysubview.superview directly, but sometimes a child view is not necessarily aware of the existence of this particular parent view, such as adding a child view dynamically.

So here we can use the message response chain pull technology.

The next thing to do is to let the child view receive these events, while the events continue to upload, will be uploaded to UIApplication until the end. In the process of passing, if the child view receives these events, then the event will naturally terminate, what we can do now is to let the child view receive the event, but also let the event does not terminate, and continue to upload.

Extract part of the description:

when user   touch screen   generate   interaction with  iphone, hardware   detects   physical contact   and   notifies   operating system. Then   OS   creates   the corresponding event   and   passes   its   to the event queue that   the currently running application. Then   This event   is passed to the   priority responder object by the event loop  . Priority Responder Object   is a   event   object that is triggered when   and   users   interact, such as   button objects and view objects. If   we   wrote   code   let   priority responders   handle   This type of event, then   it   will deal with this type of event  . After the   event has been processed, the responder   has   two options: 1,   its   discard, 2, will   its   pass to the next responder in the   response chain. The next responder's address   storage   is in the variable Nextresponder that the current responder object contains. If   Priority responder   cannot process   an event, then   This event   is passed to   next responder until   the event   arrives   can handle its responder   Or   arrives at the end of the   response chain, which is the  uiapplication type of object. UIApplication type of object   received   an event is also   either   processing, or   discarding. "

For example, there is a View object, which has a button object on it. When the user touches the button object, the button object receives an event as a priority responder. If the button object cannot handle the event, the event is passed to the View object. If the view object cannot handle this event, the event is passed to the view controller object. And so on

It should be noted that when we are using the response chain, an event does not automatically pass from one responder to the next responder. If you want to pass an event from one responder to the next, we must write code to do so. "

To do the following:

The code for the child view is as follows:

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

{

Here you can do the sub-view of what you want to do, when done, the event continues to upload, you can let its parents, even the father Viewcontroller get this event

[[Selfnextresponder]touchesbegan:toucheswithevent:event];

}

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

{

[[Selfnextresponder]touchesended:toucheswithevent:event];

}

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

{

[[Selfnextresponder] touchesCancelled:toucheswithEvent:event];

}

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

{

[[Selfnextresponder] touchesMoved:toucheswithEvent:event];

}

It is also important to note that when overriding these methods, it is best to ensure that these methods are rewritten, otherwise the event response chain may become chaotic. This is speculation, not actually verified.

iOS response event delivery, Nextresponder research

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.