First Responder 最常在我們要讓 textField 的鍵盤收起來的時候用到。
[self.view resignFirstResponder]
只知道這行程式很神奇,但到底 First Responder 是什麼東西呢?它的定義可以在 NSWindow 底下的文件找到:
The first responder is usually the first object in a responder chain to receive an event or action message. In most cases, the first responder is a view object that the user selects or activates with the mouse or keyboard.
簡單講,First Responder 是整個 responder chain 的第一個物件。
所以現在的問題是什麼 responder chain ?
在 Event Handling Guide for iOS 裡可以找到定義:
The responder chain is a series of linked responder objects. It starts with the first responder and ends with the application object. If the first responder cannot handle an event, it forwards the event to the next responder in the responder chain.
A responder object is an object that can respond to and handle events. The UIResponder class is the base class for all responder objects, and it defines the programmatic interface not only for event handling but also for common responder behavior. Instances of the UIApplication, UIViewController, and UIView classes are responders, which means that all views and most key controller objects are responders. Note that Core Animation layers are not responders.
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.