Uibutton Remove All Target-Actions

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

[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)

Why UIButton removeTarget:action:forControlEvents: has removed all targets?

As per Apple Docs:

the action parameter is not considered when stopping the delivery of events

Removing target-actions for UIButton

If you have subclassed UIButton for this or have subclassed UITableViewCell then you can put the code in there to remove the target action for the button when the cell or button is deallocated. In the UITableViewCell's dealloc you can call removeTarget on the its button, or in the button's dealloc it could call removeTarget for itself.

UIButton with multiple target actions for same event

Yes it is possible to add multiple actions to a button.

personally i would prefer a Delegate to subscribe to the button.
Let the object you want to add as target subscribe on the delegate's method so it can receive events when you press the button.

or

A single action that forwards the event to other methods to be fully in control

A simple test in swift

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

let button = UIButton(frame: CGRect(x: 50, y: 50, width: 300, height: 30))
button.backgroundColor = .orange
button.addTarget(self, action: #selector(action1), for: .touchUpInside)
button.addTarget(self, action: #selector(action2), for: .touchUpInside)
button.addTarget(self, action: #selector(actionHandler), for: .touchUpInside)
self.view.addSubview(button)
}

@objc func actionHandler(_ sender: UIButton){
print("actionHandler")
action1(sender)
action2(sender)
}

@objc func action1(_ sender: UIButton) {
print("action1")
}

@objc func action2(_ sender: UIButton) {
print("action2 \n")
}
}

Output after one click:

action1
action2

actionHandler
action1
action2

Can you confirm on the order of execution when normally adding the actions

Yes it is executed in the order you set the targets.

How do I replace the existing action of a(n?) UIButton?

You can remove a target for a specific action like this:

[button1 removeTarget: self action: @selector(oldAction) forControlEvents: UIControlEventTouchUpInside]

Or, better yet, you can remove all targets from your button like this:

[button1 removeTarget: nil action: NULL forControlEvents: UIControlEventAllEvents]

Then, you can add your new target action:

[button1 addTarget:self action:@selector(newAction) forControlEvents: UIControlEventTouchUpInside];

That's pretty much it!

Swift: Remove a UIButton attributedTitle

You need to set the AttributedTitle to nil before setting using setTitle for your button, Its working i have checked it.

like this

myButton.setAttributedTitle(nil, for: .normal)
myButton.setTitle(myRegularText, for: .normal)

How to assign different actions for same UIButton?

Add this code to check cart is already added or not, If added change title according in your detail controller:

 override func viewWillAppear(_ animated: Bool) {
if let info = detailInfo {
let buttonTItle = (self.checkCartData(cartInfo: info) ? "Go to Cart" : "Add to Cart")
addToCartButton.setTitle(buttonTItle, for: .normal)
}
}

Next, Check before adding to cart. If already added, will navigate to cart page else add new cart item(changing button title too).

@IBAction func addToCartbtnTapped(_ sender: Any) {
if let info = detailInfo {
if checkCartData(cartInfo: info) {
let cart = self.storyboard?.instantiateViewController(withIdentifier: "CartViewController") as? CartViewController
self.navigationController?.pushViewController(cart!, animated: true)
} else {
let cartData = CartStruct(cartItems: info, cartQuantity: 1)
self.saveCart(data: cartData)
showAlert()
(sender as AnyObject).setTitle("Go to Cart", for: .normal)
}
}
}

Check Cart data here:

        func checkCartData(cartInfo: jsonstruct) -> Bool {
guard let cart = self.getCartData() else { return false }
return (cart.contains(where: { $0.cartItems.name == cartInfo.name }) ? true : false )
}

Get all Cart Data with this method:

        func getCartData() -> [CartStruct]? {
let defaults = UserDefaults.standard
var tempCart: [CartStruct]?
if let cdata = defaults.data(forKey: "cartt") {
tempCart = try! PropertyListDecoder().decode([CartStruct].self, from: cdata)
}
return tempCart
}


Related Topics



Leave a reply



Submit