Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton remove all target-actions

Tags:

ios

uibutton

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?

like image 829
SK9 Avatar asked Jul 27 '10 04:07

SK9


3 Answers

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)
like image 76
progrmr Avatar answered Oct 14 '22 05:10

progrmr


@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.

like image 38
Hlung Avatar answered Oct 14 '22 07:10

Hlung


Swift 3, 4, 5:

btnCancel.removeTarget(nil, action: nil, forControlEvents: UIControlEvents.AllEvents)
like image 30
Iya Avatar answered Oct 14 '22 07:10

Iya