iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 14
0
自我挑戰組

iOS 筆記系列 第 14

什麼是 First Responder ?

  • 分享至 

  • xImage
  •  

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.

示意圖

http://ithelp.ithome.com.tw/upload/images/20161214/20103561kiIBiEXgIn.png


上一篇
Instance Method vs Class Method
下一篇
Custom HeaderView
系列文
iOS 筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言