UIButton remove all target-actions

removetarget
swift selector

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?

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

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

removeTarget:action:forControlEvents:, A target object registered with the control. Specify nil to remove the specified control events for all target objects. action. A selector identifying  UIButton remove all target-actions Call removeTarget:action:forControlEvents: , pass nil for the target, NULL for action, and use a control mask that sets all bits (UIControlEventAllEvents). Objective-C

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

UIButton remove all target-actions · GitHub, UIButton remove all target-actions. GitHub Gist: instantly share code, notes, and snippets. How do I create a basic UIButton programmatically? Set UIButton title UILabel font size programmatically ; Setting an image for a UIButton in code ; How to set the title of UIButton as left alignment? UIButton remove all target-actions

Swift:

btnCancel.removeTarget(self, action: Selector(), forControlEvents: UIControlEvents.AllEvents)

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. How to set the title of UIButton as left alignment? is it possible to adjust position x,y titleLabel of UIButton? UIButton remove all target-actions ; Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

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

Hope it helps.

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  @Praveen-K, that's incorrect. UIKit supports actions that take a second parameter, the UIEvent which triggered the action. From that you can get the list of touches, and each touch has a location. See the code I posted below. It's true that UIButton doesn't have a method that returns the location of the last click, however. – Caleb

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

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. The target object—that is, the object whose action method is called. If you specify nil, UIKit searches the responder chain for an object that responds to the specified action message and delivers the message to that object.

UIButton remove all target-actions, Question: 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  The UIButton class implements the appearance() class method, which you can use to fetch the appearance proxy for all buttons in your app. Button States Buttons have five states that define their appearance: default, highlighted, focused, selected, and disabled.

Call removeTarget:action:forControlEvents:, pass nil for the target, NULL for action, and use a control mask that sets all bits (UIControlEventAllEvents). Color Tint UIButton Image (10) I noticed that when I place a white or black UIImage into a UISegmentedControl it automatically color masks it to match the tint of the segmented control. I thought this was really cool, and was wondering if I could do this elsewhere as well.

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  I would like to change the content of a UIButton to an ActivityIndicator after it is pressed. I know buttons have an imageView and a titleLabel, but I don't know how to put an activity indicator in any of them. This is how I create activity indicators: let aiView = UIActivityIndicatorView (activityIndicatorStyle:.

Comments
  • 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.
  • 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.
  • please indicate which language are you covering
  • @JuanPabloBoero,it is swift