我想让用户用一个按钮给+1分,或者用另一个按钮加-1分,但他们应该只能按其中一个按钮一次。。。
我使用这个代码,但用户仍然可以多次点击按钮。。。
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: [])
}
由于发送方是UIButton
,因此最好构造类似的函数
@IBAction func dislikeDidTouch(_ sender: UIButton)
@IBAction func likeDidTouch(_ sender: UIButton)
在每一个里面做
sender.isEnabled = false