What is the responder chain, and what is the first responder for?
Predict first
What are the next responders in this hierarchy?
import UIKit
let v = UIView()
let a = UIView()
let child = UIView()
v.addSubview(a)
a.addSubview(child)
print(child.next === a, a.next === v)
- true true: each view’s next is its superview
- false false
Predict first
What comes after a view controller’s root view in the chain?
import UIKit
final class VC: UIViewController {}
let vc = VC()
let inner = UIView()
vc.view.addSubview(inner)
print(inner.next === vc.view, vc.view.next === vc)
- true true: the view controller
- false false
Mental model. A complaint goes to the clerk at the desk. If they cannot deal with it, it goes to their manager, then the director, until someone can.
Everything that can handle events is a UIResponder: views, view controllers, the window, the application and its delegate. Each has a next responder, forming the responder chain. For a view, next is its view controller if it is that controller’s root view, otherwise its superview; a view controller’s next is its parent or the presenter (or the window); the window’s is the application. An event that the hit-test view does not handle travels up this chain until something does, which is how a touch in a cell can end up handled by the screen’s controller.
The first responder is the object that gets keyboard input, editing commands and “target-nil” actions first. A text field becomes first responder to show the keyboard; becomeFirstResponder() asks for it and resignFirstResponder() gives it up. Actions sent with a nil target (a menu item, UIApplication.sendAction(_:to:from:for:)) start at the first responder and walk up the chain until an object implements the method, which is why the same Cut or Copy command reaches whichever view is active. Understanding the chain also explains why a controller sees events from its view and why an “orphan” view with no controller or window has a short chain.
Inside a view hierarchy the next responder is the superview, until the chain reaches the root view.
A controller’s root view hands the event to the controller itself, which then continues to its parent or presenter.
Common traps
- Assuming a button’s action goes to the controller by magic. It goes to the target you set, or up the chain if the target is
nil. - Calling
becomeFirstResponder()on a view that is not in a window. It fails, because the view must be on screen andcanBecomeFirstRespondermust be true.
They might follow up with
- What is the difference between hit testing and the responder chain? Hit testing finds the starting view by going down; the chain decides the handler by going up from it.
- How do you find the nearest view controller of a view? Walk
nextuntil you reach aUIViewController.
Related questions
- 7.1.1 How does UIKit decide which view receives a touch, and why can a subview outside its parent’s bounds not be…
- 7.1.3 How do gesture recognisers interact with touches and with each other?
- 4.4.1 What do @objc, @objcMembers, @objc(name), dynamic and @nonobjc each do?
← How does UIKit decide which view receives a touch, and why can a… How do gesture recognisers interact with touches and with each other? →