2015-05-19 89 views
0

我有一個顯示隨機表情符號的標籤,然後我有4個UIButton。每個人都會顯示一個隨機的表情符號,其中的一個會顯示與標籤上相同的表情符號。它是一個匹配的遊戲。UIBottom Text Field not setting

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var faceLabel: UILabel! 
@IBOutlet weak var scoreLabel: UILabel! 

var randomEmojiNum: UInt32 = arc4random_uniform(6) + 1 

override func viewDidLoad() { 
    super.viewDidLoad() 
    faceLabel.text = genRanNum() 
} 

func genRanNum() -> String{ 
    switch randomEmojiNum{ 
    case 1: return "" 
    case 2: return "" 
    case 3: return "" 
    case 4: return "" 
    case 5: return "" 
    case 6: return "" 
    default: break 
    } 
    return "Default" 
} 

func correctAnswerGen() { 
    var correct: UInt32 = arc4random_uniform(4) 
    switch correct{ 
    case 0: topLeftAnswer.titleLabel?.text = genRanNum() 
    case 1: topRightAnswer.titleLabel?.text = genRanNum() 
    case 2: bottomRightAnswer.titleLabel?.text = genRanNum() 
    case 3: bottomLeftAnswer.titleLabel?.text = genRanNum() 
    default: break 
    } 
} 

@IBOutlet weak var topLeftAnswer: UIButton! 
@IBOutlet weak var topRightAnswer: UIButton! 
@IBOutlet weak var bottomLeftAnswer: UIButton! 
@IBOutlet weak var bottomRightAnswer: UIButton! 
} 

但是每次我運行它,它都沒有如預期那樣。現在,我將隨機分配4個UIButton插座中的一個,將其設置爲與標籤上的表情符號相同的表情符號,但是沒有任何插座設置!

每當我運行它看起來像它被選中。沒有任何按鈕被設置!我有一個預感,它與設置的插座有關,並且在設置之後我編輯它的值。但是,如果我知道,我不會尋求幫助!哈哈歡呼!

+0

'switch random',什麼是'random'在這裏? –

+0

'correctAnswerGen' - 你還沒有叫/使用它? –

回答

2

而不是topLeftAnswer.titleLabel?.text只需使用:

topLeftAnswer.setTitle("emoji", forState: UIControlState.Normal) 
1

這是我做到了這一點:

class ViewController: UIViewController { 

    @IBOutlet weak var faceLabel: UILabel! 
    @IBOutlet weak var scoreLabel: UILabel! 

    @IBOutlet weak var topLeftAnswer: UIButton! 
    @IBOutlet weak var topRightAnswer: UIButton! 
    @IBOutlet weak var bottomLeftAnswer: UIButton! 
    @IBOutlet weak var bottomRightAnswer: UIButton! 

    var randomEmojiNum: UInt32 = arc4random_uniform(6) + 1 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     faceLabel.text = genRanNum() 
     correctAnswerGen() 
    } 

    func genRanNum() -> String{ 
     switch randomEmojiNum{ 
     case 1: return "" 
     case 2: return "" 
     case 3: return "" 
     case 4: return "" 
     case 5: return "" 
     case 6: return "" 
     default: break 
     } 
     return "Default" 
    } 

    func correctAnswerGen() { 
     var correct: UInt32 = arc4random_uniform(4) 
     switch correct{ 
     case 0: topLeftAnswer.setTitle(genRanNum(), forState: UIControlState.Normal) 
     case 1: topRightAnswer.setTitle(genRanNum(), forState: UIControlState.Normal) 
     case 2: bottomLeftAnswer.setTitle(genRanNum(), forState: UIControlState.Normal) 
     case 3: bottomRightAnswer.setTitle(genRanNum(), forState: UIControlState.Normal) 
     default: break 
     } 
    } 
} 

更新:編輯根據OP要求。

+0

它的工作原理是表情符號現在正在UIButton上打印,但是它有時只打印正確的表情符號,而其他時間則不打印任何表情符號 –

+1

我修好了!你正在傳入「正確」作爲genRanNum的參數,但是正確的是選擇打印它的UIButton的隨機數,我需要傳入randomEmojiNum! –

+0

太棒了!很高興幫助! –