由Nil-Targeted Actions說起
在開發中,做使用者互動最常用的就是target-action 模式了。但是如果target的傳入引數為nil會怎樣呢?
Apple 在UIContro.h裡給出了說明:
// passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
什麼是響應者
應用程式用來接收和響應事件的物件就是響應者。在UIKit中其程式碼表現為UIResponder或其子類的例項物件。
什麼是響應者鏈?
響應者鏈,是一條抽象鏈條,該鏈條的每個節點都是一個響應者。是通過UIResponder一個屬性串聯起來的
@property(nonatomic, readonly) UIResponder *nextResponder;
注意: UIResponder 中該屬性預設返回Nil。
不過,UIKit中UIResponder子類已經給出了一個預設實現
- 響應鏈的意義:在第一響應者不處理該事件的時候,將其傳遞個下一個響應者,直到事件被處理或因響應鏈結束沒有處理而被丟棄。
- 傳遞是怎麼進行的?
//觸控類事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesEstimatedPropertiesUpdated:(NSSet<UITouch *> *)touches NS_AVAILABLE_IOS(9_1);
//3D touch型別
- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
//運動型別
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
...
什麼是第一響應者
UIKit認為處理當前事件最合適的那個響應者就是第一響應者。所以會出現針對不同事件當前第一響應者不同的情況。比如:觸控事件的第一響應者就是觸控發生的那個檢視;搖晃手勢的第一響應者是使用者或者是UIKit指定的那個響應者。
注意:運動型事件(比如:搖晃運動)不會使用響應者鏈傳遞機制,而是直接呼叫已經指定好的第一響應者。
響應鏈條的應用:優雅的關閉鍵盤
Mac Guru Sean Heber taught us how:
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
— Sean Heber (@BigZaphod) August 1, 2012
說回問題:
target 為nil的情況下,UIKit會先去找已經指定的第一響應者;如果沒有,則呼叫使用者觸控的檢視。如果事件沒有處理則沿著響應者鏈往下傳遞。
參考連結:
相關文章
- SciTech-BigDataAIML-LLM-Transformer Series-Self-Attention:由Dot-Product(向量點乘)說起AIORM點乘
- 由Nodejs來說I/ONodeJS
- 從 JSON 說起JSON
- 從噠噠英語刷單說起:創業公司資料造假的鍋,該由誰來背?創業
- iOS逆向——從RSA說起iOS
- 從SEQUENCE跳號說起
- 從測試說起(二)
- 習鐵由治委到體寫起小cug
- 曹工說Tomcat1:從XML解析說起TomcatXML
- 說多了都是淚--由開發自主選型
- Github Actions 初探Github
- 由iphone12說說鐳射雷達 FMCW鐳射雷達iPhone
- 從 CALayer 的 Position、AnchorPoint 說起
- 從 RouterModule.forRoot 方法說起
- 單機遊戲會和單機一起消失嗎?——由谷歌Stadia談起遊戲谷歌
- Git 由淺入深之細說變基 (rebase)Git
- 從兩道面試題說起面試題
- zanphp原始碼解讀 – MVC說起PHP原始碼MVC
- 從Kotlin的類開始說起Kotlin
- 從《死亡擱淺》6.8分說起
- 從 gRPC 的重試策略說起RPC
- AR,我們從設計說起
- 叢集通訊:從心跳說起
- 從用SwiftUI搭建專案說起SwiftUI
- 由《尋秦記》說代理模式(靜態,動態,CGLib)模式CGLib
- 夯實Java:從物件導向說起Java物件
- 從concurrent下的Atomic原子類說起
- 從救貓還是救畫說起
- 決策樹詳解,從熵說起熵
- 從滅霸的無限手套說起
- JavaScript 事件迴圈(1) —— 從 setTimeout 說起JavaScript事件
- 從容器映象的選擇-alpine 說起
- 從淘寶首頁登入說起
- FinOps實踐,從降本增效說起
- GitHub Actions 入門指南Github
- GitHub Actions 入門教程Github
- 強大的Github ActionsGithub
- github actions自動部署Github
- 英語流利說懂你英語 Level5 Unit3 Part3 Vocabulary - Describing Actions