Ios UIButton sender.titleLabel?.text?.toInt()在按下过快时给出未更新的值

Ios UIButton sender.titleLabel?.text?.toInt()在按下过快时给出未更新的值,ios,iphone,swift,uibutton,uicontrolstate,Ios,Iphone,Swift,Uibutton,Uicontrolstate,在我的应用程序中,我按下按钮,它会用iOS默认按钮动画更改背景颜色和类似的数字。按钮的默认行为类似于数字,只能在区间[x,x+1]或[x-1,x]内,其中x为初始值。但是,如果快速按下按钮,类似的数字就会快速增加或快速减少 func likeButtonAction(sender:UIButton!) { var oldValue = sender.titleLabel?.text?.toInt() println("oldvalue \(oldValue)")

在我的应用程序中,我按下按钮,它会用iOS默认按钮动画更改背景颜色和类似的数字。按钮的默认行为类似于数字,只能在区间[x,x+1]或[x-1,x]内,其中x为初始值。但是,如果快速按下按钮,类似的数字就会快速增加或快速减少

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)")
        }
}
编辑1: 当快速按下时,输出为:

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
编辑2: 解决方案: 这工作正常,但我不知道为什么。请解释

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)
        }
}

从那时起,这将不起作用

 if sender.backgroundImageForState(UIControlState.Normal) == UIImage(named: "like.png")
他总是错的

UIImage(名为:“like.png”)

将创建新的UIImage实例


您可以将“标记”分配给任何视图(在本例中为UIButton)以处理此类情况。

我的建议是将.Normal和.Selected的按钮状态分别设置为“like.png”和“like.png”。然后,您的代码可以简单到:

func likeButtonAction(sender:UIButton!) {
        if sender.selected {
            //upvote
            sender.setTitle(String(oldValue! + 1), forState: UIControlState.Normal)
            sender.setTitleColor(MyStyle.ThemeSecondColor, forState: UIControlState.Normal)
        } else {
            //downvote
            sender.setTitle(String(oldValue! - 1), forState: UIControlState.Normal)
            sender.setTitleColor(MyStyle.ThemeColor, forState: UIControlState.Normal)
        }
        //Quickly flip the button state to the opposite of what it was
        sender.selected = !sender.selected
}
编辑:

嗯,您可以实现一个变量来保存“count”并首先设置它,然后用它设置按钮的值,而不是将值拉入和拉出按钮标题:

var likeCount:Int = [SET THIS FROM YOUR STORAGE] || 0
func likeButtonAction(sender:UIButton!) {
    if sender.selected {
        //upvote
        likeCount++
        println("inc \(likeCount!)")

    } else {
        //downvote, but do not allow negative values
        if likeCount == 0{
            likeCount = 0
        } else {
            likeCount--
        }
        println("dec \(likeCount!)")
    }
    sender.setTitle(String(likeCount!), forState: UIControlState.Normal)
    sender.selected = !sender.selected
}

这将保持对值的单独跟踪,并防止UI可能阻碍流程

谢谢,但没用。当快速按下时,静止数字变化不准确。以这种方式进行更改,但问题仍然存在。所以我的假设是错误的,我将编辑这个问题