如何将字典值数组显示为按钮?

时间:2017-07-21 05:45:43

标签: ios swift xcode dictionary uibutton

我正在开发一个iOS测验应用程序,其中的问题和答案存储在[String:Array]类型的字典中,其中问题是关键字,问题的答案是其值。我已经找到了如何通过创建字典键的数组并将标签文本设置为随机选择键来随机显示问题。但是,我无法将所选键的值设置为按钮的标题?每个键有四个值和四个按钮,将答案值显示为标题作为按钮的最有效和最简单的方法是什么?

这是我的代码(当我运行模拟器时,四个按钮标题只是说“按钮”,即使我的代码中有setTitle函数):

class QuestionDetails {

    let QADictionary = [
        "Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"],
        "What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"],
        "Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]
    ]

}

//get question list
func questionWithAnswers() {
    let listQuestions = QuestionDetails()

    var questionList = Array(listQuestions.QADictionary.keys)

    //random question index
    var rand = Int(arc4random_uniform(UInt32(questionList.count)))

    var randAnswers = rand
    //random answer index
    var answerList = Array(listQuestions.QADictionary.values)

    var choices = answerList[randAnswers]

    //fetch questions from list
    let question = questionList[rand]

    questionLabel.text = question

    //function for new question and button titles
    func showQuestion() {
        rightAnswerBox = arc4random_uniform(4)+1

        //create button
        var button:UIButton = UIButton()
        var x = 1

        for index in 1...4
        {
            button = view.viewWithTag(index) as! UIButton

            if (index == Int(rightAnswerBox))
            {
                button.setTitle(choices[0], for: .normal)

            }

            else {
                button.setTitle(choices[x], for: .normal)
                x += 1

            }
            randomImage()
        }
    }
}

2 个答案:

答案 0 :(得分:1)

首先为故事板中的所有四个按钮设置标记,或使用以下代码:

button1.tag = 1
button2.tag = 2 // and so on... for button3, button4

我已经编辑了你的代码,删除了你没有调用的函数 showQuestion(),现在它正常工作。

//get question list
func questionWithAnswers() {

    let listQuestions = QuestionDetails()
    var questionList = Array(listQuestions.QADictionary.keys)

    //random question index
    let rand = Int(arc4random_uniform(UInt32(questionList.count)))

    var randAnswers = rand
    //random answer index
    var answerList = Array(listQuestions.QADictionary.values)

    var choices = answerList[randAnswers]

    //fetch questions from list
    let question = questionList[rand]

    questionLabel.text = question

    //function for new question and button titles
    let rightAnswerBox = arc4random_uniform(4)+1

    //create button
    var button:UIButton
    var x = 1

    for index in 1...4
    {
        button = view.viewWithTag(index) as! UIButton

        if (index == Int(rightAnswerBox))
        {
            button.setTitle(choices[0], for: .normal)

        }

        else {
            button.setTitle(choices[x], for: .normal)
            x += 1

        }
        //randomImage()
    }

}

答案 1 :(得分:0)

Ypu可以使用folleowing代码来维护一组按钮,并使用故事板或笔尖中的tis数组连接它们的多个按钮:

@IBOutlet var collectionOfButtons: Array<UIButton>?

Here您可以找到更详细的示例。