UIControl.sendAction 的选择器的目的是啥?

Posted

技术标签:

【中文标题】UIControl.sendAction 的选择器的目的是啥?【英文标题】:What is the purpose of UIControl.sendAction's selector?UIControl.sendAction 的选择器的目的是什么? 【发布时间】:2015-10-27 00:04:56 【问题描述】:

UIControl.sendAction 的选择器参数的目的是什么? UIControl.addTarget 接受选择器参数表示发送操作时要调用的函数是有道理的,但是为什么发送事件的控制器会指定要调用的函数呢?我缺少一些东西。在某些情况下,这是我尝试实现的自定义按钮的一段代码:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) 
    self.pressed = false
    if self.bounds.contains(touches.first!.locationInView(self)) 
        // sendAction(???, to: nil, forEvent: UIControlEvents.TouchUpInside)
    

    super.touchesEnded(touches, withEvent: event)

【问题讨论】:

【参考方案1】:

它不适合你。基本上,你已经看到了幕后。现在把目光移开。系统调用此方法。你永远不应该打电话给它。理论上你可以覆盖它,但你永远不会这样做。

想发送一个控制事件对应的动作时,你调用sendActionsForControlEvents:

【讨论】:

如果我调用 sendAction,Apple 会拒绝我的申请吗?

以上是关于UIControl.sendAction 的选择器的目的是啥?的主要内容,如果未能解决你的问题,请参考以下文章