ChatGPT解决这个技术问题 Extra ChatGPT

UIButton remove all target-actions

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?


V
Ved Sharma

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)

Thanks for the tip! Here's the full link I think (ie. to the section): "developer.apple.com/iphone/library/documentation/uikit/…:"
progrmr's suggestion works of course. To add to the answer here is a code snippet like the one I needed: [button removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
Aside: the -allTargets instance method returns an NSSet of all the instance's targets (nil if none).
Perfect! exactly what i was looking for:D
Updated with Swift 2 and 3, as there are competing answers with exactly the same answer, differing only in language.
H
Hlung

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


For Swift 3: ".AllEvents" is now ".allEvents" (with a lowercase 'a'): removeTarget(nil, action: nil, for: .allEvents)
Regarding your NOTE: Actually, I believe you could pass either nil or NULL to both the first and second arguments in Objective-C too, and it will work. I believe both are defined as (void*) 0 (or at the very least, evaluate as equals).
@Sasho I would love to see some statistics on how much source files sizes go down (on average) when upgrading to Swift 3 :-) (due to the new method/argument naming rules).
Since this is the same answer in a different language, and the language is not tagged in this question, it should be an edit to @progrmr's answer.
M
Mohit Kumar

Swift 3, 4, 5:

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

please indicate which language are you covering
@JuanPabloBoero,it is swift
T
Tim Langner

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

U
UdayM
- removeTarget:action:forControlEvents:

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

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


D
Divesh singh

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)