由Nil-Targeted Actions说起

作者: 2d899c5242bd | 来源:发表于2017-09-25 00:22 被阅读47次

在开发中,做用户交互最常用的就是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子类已经给出了一个默认实现

App中的默认响应者链
  • 响应链的意义:在第一响应者不处理该事件的时候,将其传递个下一个响应者,直到事件被处理或因响应链结束没有处理而被丢弃。
  • 传递是怎么进行的?
//触摸类事件
- (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会先去找已经指定的第一响应者;如果没有,则调用用户触摸的视图。如果事件没有处理则沿着响应者链往下传递。


参考链接:

  1. https://www.cocoanetics.com/2012/09/the-amazing-responder-chain/
  2. https://developer.apple.com/documentation/uikit/understanding_event_handling_responders_and_the_responder_chain?language=objc

相关文章

  • 由Nil-Targeted Actions说起

    在开发中,做用户交互最常用的就是target-action 模式了。但是如果target的传入参数为nil会怎样呢...

  • selenium鼠标事件

    Actions actions = new Actions(driver); actions.click...

  • dcat admin 添加自定义操作按钮

    $grid->actions(function (Grid\Displayers\Actions $actions...

  • 由垃圾说起……

    由垃圾说起… 一个敏感的字眼, 一个可触痛 各条神经的字眼, 不在天边, 就在你的眼前。 不管外表多么光鲜, 垃圾...

  • 由背影说起……

    我慢慢地、慢慢地了解到,所谓父女母子一场,只不过意味着,你和他的缘分就是今生今世不断地在目送他的背影渐行渐远。 ...

  • 由切腹说起

    前些天和几个台湾人闲聊,谈到了日本侵略对台湾的影响,顺而谈到日本,谈到了日本人的一些风俗,也谈到了日本人独特...

  • 由“寒蝉”说起

    昨晚,阅读孩子在学校写的作文,看到中间有一句“寒蝉凄切,秋雨初歇”,我问孩子:“寒蝉”代表什么季节,是秋天吗?”孩...

  • 由“种植”说起

    最近,学校将后院的闲置土地资源进行了规整,开辟出了可供学生种植、观察、实验的田园。连续数周,老师,学生和家长群策群...

  • 由苜蓿说起

    有人说苜蓿好吃极了,胜过一切美味,我一直怀疑那种过犹不及和变态礼赞。我把它当做寻常野菜来看,在油腻荤菜过多的“食代...

  • 由零说起

    说起归零,这个名词是我上警校时教我们经济侦查老师课的老师教授的,当时还比较懵懂,想来体会这个词已经快十年了。至今还...

网友评论

    本文标题:由Nil-Targeted Actions说起

    本文链接:https://www.haomeiwen.com/subject/tinlextx.html