Search code examples
iosuicontrol

What is the purpose of UIControl.sendAction's selector?


What is the purpose of UIControl.sendAction's selector argument? It makes sense that UIControl.addTarget takes the selector argument that represents the function that is to be called when an action is sent, but why would the controller that's sending the event specify a function to be called? There's something that I'm missing. For some context, here's a piece of code from the custom button that I'm trying to implement:

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)
}

Solution

  • It's not for you. Basically, you've looked behind the curtain. Now look away. The system calls this method. You should never call it. In theory you could override it but you're never going to.

    When you want to send the action corresponding to a control event, you call sendActionsForControlEvents:.