Search code examples
iosswiftselectoruicontrol

What is the difference between addTarget(_:action:for:) and sendAction(_:to:for:) UIControl instance methods?


I am subclassing UIControl and as I wanted to set my view controller as the target, I discovered the sendAction method. I can't really figure out the differences between both methods and when their respective usage is more appropriate.


Solution

  • The difference is that sendAction(_:to:for:) actually calls the defined selector right away, while addTarget(_:action:for:) only associates a target and action with the control and only calls the selector when the event happens.