在我的应用程序中,我按下了按钮,它使用iOS默认按钮动画更改了backgroundcolor和类似的数字。按钮的默认行为就像数字只能位于区间[x,x + 1]或[x-1,x]中,其中x是初始值。但是,如果按钮被快速按下,相似的数字就会迅速增加或减少。当按下太快时,UIButton sender.titleLabel?.text?.toInt()不会提供更新值
func likeButtonAction(sender:UIButton!) {
var oldValue = sender.titleLabel?.text?.toInt()
println("oldvalue \(oldValue)")
if sender.selected {
//upvote
sender.setTitle(String(oldValue! + 1), forState: UIControlState.Normal|UIControlState.Selected)
println("inc \(oldValue! + 1)")
} else {
//downvote
sender.setTitle(String(oldValue! - 1), forState: UIControlState.Normal|UIControlState.Selected)
println("dec \(oldValue! - 1)")
}
}
EDIT1: 当按下迅速输出:
oldvalue Optional(3)
inc 4
oldvalue Optional(4)
dec 3
oldvalue Optional(3)
inc 4
oldvalue Optional(4)
dec 3
oldvalue Optional(4)
inc 5
oldvalue Optional(5)
dec 4
oldvalue Optional(5)
inc 6
oldvalue Optional(6)
dec 5
oldvalue Optional(6)
inc 7
oldvalue Optional(7)
dec 6
oldvalue Optional(7)
inc 8
oldvalue Optional(8)
dec 7
oldvalue Optional(8)
inc 9
oldvalue Optional(9)
dec 8
oldvalue Optional(8)
inc 9
EDIT2: SOLUTION: 这个工作正常,但我不知道为什么。解释将不胜感激
func likeButtonAction(sender:UIButton!)
if sender.selected {
//upvote
sender.setTitle(String((sender.titleLabel?.text?.toInt())! + 1), forState: UIControlState.Normal|UIControlState.Selected)
} else {
//downvote
sender.setTitle(String((sender.titleLabel?.text?.toInt())! - 1), forState: UIControlState.Normal|UIControlState.Selected)
}
}
改变了这种方式,但问题仍然存在。所以我的假设是错误的我会编辑 – Thellimist 2015-02-06 15:18:27