Who are Responders?
They are the common classes that deals with responding to touches, motion events and etcs.
Who is the FIRST responder?
When user tap on a view, the system has to first find out who is the first responder?
This is known as a hit-test phase.
A hit testing phase starts from the lowest level (the window).
Hit test traverse up, while responder chain traverse down.
hitTest:withEvent: will be called recursively into subviews (traverse up), until it reaches the leaf view. That responder is the first.
After identifying the first responder, then the responder chain begins, starting with the first responder of course.
There are rules to finding the next responder.
- The first responder passes the event to its view controller if it has one; if not, it passes to its superview.
- If a view or its view controller cannot handle the event, it passes to the next responder (superview or parent view controller).
- Each subsequent superview in the hierarchy follows the pattern described in the first two steps if it cannot handle the event or message.
- Topmost view in the view hierarchy, or next
- UIWindow, or next
- Lastly, App Delegate
The target-action trick
You can set a target to nil, and it will traverse up the responder chain to find the action.
An example from swiftandpainless.com.