UIButton remove all target-actions

Viewed 88615

I have added multiple target-action-forControlEvents: to a UIButton. I'd like to remove all of these in one go without deallocating anything. I will then set new targets.

Is this possible and how do I go about it?

6 Answers

Swift 2:

actionButton.removeTarget(nil, action: nil, forControlEvents: .AllEvents)

Swift 3 & 4:

actionButton.removeTarget(nil, action: nil, for: .allEvents)

Objective-C:

[actionButton removeTarget: nil action: NULL forControlEvents: UIControlEventAllEvents];

you can change the selector if it is conditional. see below example

you can remove all the targets first, then choose the selector and add it.

rateButton.removeTarget(nil, action: nil, for: .allEvents)

    let action = interview.isRated ? #selector(viewTapped(_:)) : #selector(rateTapped(_:))
            
    rateButton.addTarget(self, action: action, for: .touchUpInside)
Related