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