UIKitMidMCQ
What is the `UIResponder` and the responder chain?
Test your knowledge:
Explanation & Code
Answer:
The responder chain is a sequence of UIResponder objects that can handle events (touches, key presses, actions). Events travel up the chain until something handles them.
UIView → parent UIView → UIViewController → UIWindow → UIApplication → AppDelegate
Code Example:
// Custom action sent up the responder chain
// No need to know which object handles it
UIApplication.shared.sendAction(#selector(handleLogout), to: nil, from: nil, for: nil)
// Any UIResponder in the chain can handle it
class RootViewController: UIViewController {
@objc func handleLogout() {
// Handles the action wherever it is in the chain
AuthManager.shared.logout()
}
}
// First responder — the view currently receiving input
textField.becomeFirstResponder() // show keyboard
textField.resignFirstResponder() // hide keyboard
Rate your understanding: