Swift:"奇怪"的事件响应链

初学iOS开发的童鞋可能会遇到各式奇怪的问题,这篇博文本猫就为大家介绍其中一个”怪异”问题.

该问题很好重现: 
1.使用xcode新建一个最简单的单vc工程,然后在Storyboard中新建一个destVC.然后从默认创建的VC(姑且称之为initVC)拉一条segue到destVC,并设置该segue ID为”ToDestVC”

2.打开initVC类,只添加2个方法:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        NSLog("在destVC中触摸屏幕却会执行initVC中的这句代码")
    }

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    performSegue(withIdentifier: "ToDestVC", sender: nil)
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.好了,只需要修改这么多.现在编译运行app,由于上面代码的原因,首先initVC被显示,但很快切换到destVC中去.此时在destVC的视图中轻触屏幕,你会发现却是initVC中的touchesBegan方法被触发执行了,是否有些丈二和尚摸不着头脑呢?如果不信,请自行按照以上步骤操作查看结果.

究竟为何会这样呢?其实apple的开发文档中早就说明了原因:

Understanding Event Handling, Responders, and the Responder Chain

如果不想看长篇大论,可以直接看最后这一段话:

这里写图片描述

说的很清楚吧,所以如果不用segue跳转,而是用present跳转到destVC,现象是一样的.

知道了原因如果避免这一”正常”的行为呢?很简单,一种办法是在initVC的prepare方法中关闭initVC.view的交互:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ToDestVC"{
            view.isUserInteractionEnabled = false
        }
    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5

另一种方法则是在destVC中重载next属性,将其置为nil,断开responder chain即可:

override var next: UIResponder?{
        return nil
    }
   
   
  • 1
  • 2
  • 3
版权声明:大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.谢谢! hopy ;)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
接着分析 (result (type_ident (component id='Bool' bind=Swift.(file).Bool))) (brace_stmt range=[re.swift:1:59 - line:14:1] (pattern_binding_decl range=[re.swift:2:5 - line:2:33] (pattern_named type='[UInt8]' 'b') Original init: (call_expr type='[UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:33] nothrow (constructor_ref_call_expr type='(String.UTF8View) -> [UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:19] nothrow (declref_expr implicit type='(Array<UInt8>.Type) -> (String.UTF8View) -> Array<UInt8>' location=re.swift:2:19 range=[re.swift:2:19 - line:2:19] decl=Swift.(file).Array extension.init(_:) [with (substitution_map generic_signature=<Element, S where Element == S.Element, S : Sequence> (substitution Element -> UInt8) (substitution S -> String.UTF8View))] function_ref=single) (argument_list implicit (argument (type_expr type='[UInt8].Type' location=re.swift:2:13 range=[re.swift:2:13 - line:2:19] typerepr='[UInt8]')) )) (argument_list (argument (member_ref_expr type='String.UTF8View' location=re.swift:2:29 range=[re.swift:2:21 - line:2:29] decl=Swift.(file).String extension.utf8 (declref_expr type='String' location=re.swift:2:21 range=[re.swift:2:21 - line:2:21] decl=re.(file).check(_:_:).encoded@re.swift:1:14 function_ref=unapplied))) )) Processed init: (call_expr type='[UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:33] nothrow (constructor_ref_call_expr type='(String.UTF8View) -> [UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:19] nothrow (declref_expr implicit type='(Array<UInt8>.Type) -> (String.UTF8View) -> Array<UInt8>' location=re.swift:2:19 range=[re.swift:2:19 - line:2:19] decl=Swift.(file).Array extension.init(_:) [with (substitution_map generic_signature=<Element, S where Element == S.Element, S : Sequence> (substitution Element -> UInt8) (substitution S -> String.UTF8View))] function_ref=single) (argument_list implicit (argument (type_expr type='[UInt8].Type' location=re.swift:2:13 range=[re.swift:2:13 - line:2:19] typerepr='[UInt8]')) )) (argument_list (argument (member_ref_expr type='String.UTF8View' location=re.swift:2:29 range=[re.swift:2:21 - line:2:29] decl=Swift.(file).String extension.utf8 (declref_expr type='String' location=re.swift:2:21 range=[re.swift:2:21 - line:2:21] decl=re.(file).check(_:_:).encoded@re.swift:1:14 function_ref=unapplied))) ))) (var_decl range=[re.swift:2:9 - line:2:9] "b" type='[UInt8]' interface type='[UInt8]' access=private readImpl=stored writeImpl=stored readWriteImpl=stored)
最新发布
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值