UIButton remove all target-actions

IosUibutton

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

Call removeTarget:action:forControlEvents:, pass nil for the target, NULL for action, and use a control mask that sets all bits (UIControlEventAllEvents).

Objective-C

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

Swift 2

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

Swift 3 or higher

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

Solution 2 - Ios

@progrmr's answer in Swift 2:

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

and Swift 3:

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

Note: Swift doesn't have NULL, so I tested replacing it with nil and it seems to work fine.

Solution 3 - Ios

Swift 3, 4, 5:

btnCancel.removeTarget(nil, action: nil, forControlEvents: UIControlEvents.AllEvents)

Solution 4 - Ios

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];

Solution 5 - Ios

- removeTarget:action:forControlEvents:

This method stops the delivery of events to the specified target object.

  1. Specifying a valid object in the target parameter, this method stops the delivery of the specified events to all action methods associated with that object.

  2. Specifying nil for the target parameter, this method prevents the delivery of those events to all action methods of all target objects

objective-c:

    [_myButton removeTarget:  //any validObject (or) nil
                  action:nil
        forControlEvents:UIControlEventAllEvents]; 

swift:

    myButton.removeTarget(*validObject or nil*, action:nil, forControlEvents:UIControlEvents.AllEvents)

For more details https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/index.html#//apple_ref/occ/instm/UIControl/removeTarget:action:forControlEvents:

Solution 6 - Ios

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSK9View Question on Stackoverflow
Solution 1 - IosprogrmrView Answer on Stackoverflow
Solution 2 - IosHlungView Answer on Stackoverflow
Solution 3 - IosIyaView Answer on Stackoverflow
Solution 4 - IosRiajur RahmanView Answer on Stackoverflow
Solution 5 - IosUdayMView Answer on Stackoverflow
Solution 6 - IosDivesh singhView Answer on Stackoverflow