tezvyn:

The Responder Chain: Who Handles That Tap?

AI-drafted, machine-checkedSource: developer.apple.comadvanced
The Responder Chain: Who Handles That Tap?

The Responder Chain is iOS's chain of command for events. When a user taps the screen, the event travels up a view hierarchy, asking each object, "Can you handle this?" until one accepts. Accidentally breaking this chain can silently kill event handling.

WHY IT EXISTS: In a complex UI with nested views, multiple objects might be eligible to respond to a single user action. The system needs a deterministic, predictable way to decide which object gets the final say, preventing ambiguity and ensuring events are handled cleanly.

THE MENTAL MODEL: Think of it as passing a request up a chain of command. An event, like a finger tap, starts at the most specific object (the view that was tapped) and travels "up" a pre-defined path of potential responders. Each object gets a chance to handle the event. If it declines, the event continues up the chain until an object handles it or the chain ends.

HOW IT WORKS: When an event occurs, the system first determines the "first responder"—usually the view directly under the user's finger. If this view doesn't handle the event, it passes it to its next responder. For a UIView, the next responder is typically its superview. If the view is managed by a UIViewController, its next responder is the controller. The chain continues from view to superview, to the root view, to the view controller, to its parent, and eventually to the UIWindow and UIApplication object. An object "handles" an event by implementing the corresponding method, like touchesBegan(_:with:).

WHEN TO USE IT: You interact with the responder chain constantly, even if implicitly. Every time you create an @IBAction for a button, you're plugging into this system. You might explicitly manipulate it to manage focus for text input (making a text field the first responder to show the keyboard) or to handle custom touch events that need to be processed by a parent container rather than the specific subview that was touched.

WHEN NOT TO USE IT: Don't use the responder chain for direct communication between two specific components. If a child view needs to tell its specific parent controller something, a delegate pattern or a closure is more explicit and less fragile. The responder chain is for broadcasting an event upwards to an unspecified handler, not for targeted messaging.

ONE CANONICAL EXAMPLE: A UITextField inside a UITableViewCell. When tapped, the text field becomes the first responder and shows the keyboard. It handles typing events. If an unhandled event occurs, like a device shake for "Undo," the text field passes it up the chain to its superview, the cell, the table view, and eventually to the view controller, which might present the undo UI.

Read the original → developer.apple.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.