1

I am reading through the documentation of UIResponder class and I came across the responder chain. Now it's said that for example, when a hit-test can't handle an event, it passes it up a chain of responders. So, what example can clarify how an event can't be handled?

Badr
  • 461
  • 4
  • 18

1 Answers1

2

From the Event Handling Guide for iOS

If the initial object—either the hit-test view or the first responder—doesn’t handle an event, UIKit passes the event to the next responder in the chain. Each responder decides whether it wants to handle the event or pass it along to its own next responder by calling the nextResponder method. This process continues until a responder object either handles the event or there are no more responders.

What "handling" an event means is really up to each responder class. The decision you need to make when implementing a responder is, for each event, do you pass it along to the next responder or not.

There's also an important note at the bottom of that section:

Important: If you implement a custom view to handle remote control events, action messages, shake-motion events with UIKit, or editing-menu messages, don’t forward the event or message to nextResponder directly to send it up the responder chain. Instead, invoke the superclass implementation of the current event handling method and let UIKit handle the traversal of the responder chain for you.

Similarly for the most common case where your responder is a UIView subclass working with touch events, all of those methods include:

The default implementation of this method does nothing. However immediate UIKit subclasses of UIResponder, particularly UIView, forward the message up the responder chain. To forward the message to the next responder, send the message to super (the superclass implementation); do not send the message directly to the next responder. For example,

[super touchesBegan:touches withEvent:event];

If you override this method without calling super (a common use pattern), you must also override the other methods for handling touch events, if only as stub (empty) implementations.

Community
  • 1
  • 1
Jonah
  • 17,609
  • 1
  • 41
  • 68
  • so, Why would a responder not be able to handle it so in that case I pass it to the next one? I guess it differs according to the type of the event but, I need an example for it. thanks – Badr Jul 25 '16 at 00:29
  • Consider a game board. An individual piece on the game board might react to touch events so that it can respond to a tap gesture but you might want a swipe or drag gesture to be handled be the board so it can manage interactions between pieces. – Jonah Jul 25 '16 at 00:31
  • okay, so it's according to the coder's purpose if he wants this responder or other to handle the event? – Badr Jul 25 '16 at 00:33
  • Yes, when you create a responder you need to decide how it reacts to events and if it allows those events to continue along the responder chain so that other responders will receive them. – Jonah Jul 25 '16 at 00:37