簡體   English   中英

太快按下時,UIButton sender.titleLabel?.text?.toInt()不會提供更新的值

[英]UIButton sender.titleLabel?.text?.toInt() gives not updated value when pressed too fast

在我的應用程序中,我按下按鈕,它使用iOS默認按鈕動畫來更改背景顏色和類似數字。 按鈕的默認行為就像number只能在間隔[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)")
        }
}

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:解決方案:這正常工作,但我不知道為什么。 解釋將不勝感激

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”和“ liked.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潛在地阻礙該過程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM