2014-12-26 31 views
6

我有一個按鈕,按鈕的標題是一個整數。檢查Swift中的UIButton標題

correctAnswer = randomNumber1 + randomNumber2 
button1.setTitle("\(correctAnswer)", forState: UIControlState.Normal) 

我想檢查一下按鍵的標題所以它轉換爲整數,如果語句中使用它在,但不能使它工作。

button1.titleLabel?.text?.toInt() 
    if button1.titleLabel == correctAnswer { //most probably I am making a mistake here. 
     scoring() } else { 
     println(button1.titleLabel?.text!) 

我是新來的swift,我甚至可能有問題,我的方法,您的幫助將不勝感激。

回答

18

你應該使用button1.currentTitle

+0

我嘗試過,但此給出了一個錯誤'button1.currentTitle == correctAnswer'錯誤是:「串」類型不符合protocol'_RawOptionSetType' – user3499983

+0

然後嘗試button1.currentTitle.toInt()! == correctAnswer – ChikabuZ

+0

現在非常感謝它。 – user3499983

1

也應該使用

sender.currentTitle