J. Doe
J. Doe

Reputation: 285

let user press button only one time

I want to let user give +1 with one button or -1 points with another button, but they should be able to only press one of these buttons one time...

I use this code, but the user can still click on the button multiple times...

var job: Job! {
    didSet {
        jobLabel.text = job.text
        likeButton.setTitle("👍 \(job.numberOfLikes)", for: [])
        dislikeButton.setTitle("👎 \(job.numberOfDislikes)", for: [])
    }
}

@IBAction func dislikeDidTouch(_ sender: AnyObject)
{
    (dislikeDidTouch).isEnabled = false
    job.dislike()
    dislikeButton.setTitle("👎 \(job.numberOfDislikes)", for: [])
    dislikeButton.setTitleColor(dislikeColor, for: [])    }

@IBAction func likeDidTouch(_ sender: AnyObject)
{
    sender.userInteractionEnabled=YES;
    job.like()
    likeButton.setTitle("👍 \(job.numberOfLikes)", for: [])
    likeButton.setTitleColor(likeColor, for: [])
}

Upvotes: 0

Views: 578

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

Since the sender is a UIButton , it's better to construct the funcs like this

@IBAction func dislikeDidTouch(_ sender: UIButton)
@IBAction func likeDidTouch(_ sender: UIButton)

and inside each one do

sender.isEnabled = false

Upvotes: 1

Related Questions