John Doah
John Doah

Reputation: 1999

Button is not working, not detecting a click, and detecting which button is clicked

I have a custom cell class for my tableView I created two button a minus and plus button. I wrote a func, and I want to use the same function for both buttons. This is what I have now:

private let decreaseButton: UIButton = {
    let btn = UIButton(type: .custom)
    btn.setImage(UIImage(named: "minusIcon"), for: .normal)
    btn.imageView?.contentMode = .scaleAspectFit
    btn.addTarget(self, action: #selector(ChangePlayerRank), for: .touchUpInside)
    return btn
}()

this is ChangePlayerRank:

@objc func ChangePlayerRank(){
    print("BUTTON WORK")
}

When clicked a button, it does not detect it. plus, how can I detect which of the two buttons I have (deacrease and increase), and depending on which one was click, the method will decrease a point or increase a point? I tried which sender and tag but it didn't work.

Upvotes: 0

Views: 39

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You need to add a parameter , and set 2 different tags for the button

@objc func changePlayerRank(_ bt:UIButton) {

    if bt.tag == 10 { // minus // or bt == decreaseButton  ( without tags )

    else {   // plus

     }  

}

To access self

lazy var decreaseButton: UIButton = {

Upvotes: 1

Related Questions