How do Responder Chain Works in IPhone? What are the "next responders"?

Viewed 21968

This is what the documentation says:

If the first responder [to an event or action message] cannot handle an event or action message, it forwards it to the “next responder” in a linked series called the responder chain. The responder chain allows responder objects to transfer responsibility for handling an event or action message to other objects in the application.

If an object in the responder chain cannot handle the event or action, it resends the message to the next responder in the chain. The message travels up the chain, toward higher-level objects, until it is handled. If it isn't handled, the application discards it.

Okay, what is the next responder?

Is it the parent view? The view behind it? How does iOS decide what is the first responder and second responder?

5 Answers

Apps receive and handle events using responder objects.

A responder object is any instance of the UIResponder class,

  • common subclasses include

    UIView, UIViewController, and UIApplication.

Responders receive the raw event data and must either handle the event or forward it to another responder object.

When your app receives an event, UIKit automatically directs that event to the

  • most appropriate responder object, known as the

    first responder.

Unhandled events are passed from responder to responder in the active responder chain,

which is the dynamic configuration of your app’s responder objects.

Now look at the below screen-shot, Also consider the View-Hierarchies from the front:

img

UIbutton/UITextField --(nextResponder)-> UIView --(nextResponder)-> UIViewController

--(nextResponder)-> UIWindow --(nextResponder)-> UIApplication --(nextResponder)-> UIApplicationDelegate

That is how the Responder chain works on iOS, hope it will help anyone Also the latest article on Apple's website is --> Link (Very Well explained.)

Related