2

I am developing a iOS quiz app where the questions and answers are stored in a dictionary of type [String : Array] where the questions are the keys and the answers to that question are its values. I have found out how to randomly display question by creating an array of the dictionary's keys and setting a label text as the the random chosen key. However I am having trouble setting the values of the chosen key as title's for the buttons? There are four values per key and four buttons, what would be the most efficient and simplest way to display the answer values as titles as buttons?

Here is my code (when I run the simulator the four buttons titles just say "button" even though I have a setTitle function in my code):

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()
        }
    }
}
Bista
  • 7,869
  • 3
  • 27
  • 55
Captain Code
  • 277
  • 1
  • 3
  • 13
  • There are so many ways, from create collection view to set button in your storyboard, why use tags instead of just create IBOutlet for each of them? – Tj3n Jul 21 '17 at 05:52
  • could you give one of those many examples then in code? because I can't seem to get it right – Captain Code Jul 21 '17 at 06:01

2 Answers2

1

First set the tag for all four buttons either in storyboard or using code as below:

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

I have edited your code, removed the function showQuestion(), which you have not called and now it's working fine.

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

}
Rahul Kumar
  • 3,009
  • 2
  • 16
  • 22
  • Thank you! The code worked once I removed the function like you suggested. Is there a way I could remove a key from the array of keys once one of the questions have been answered? – Captain Code Jul 21 '17 at 18:59
  • Cool..., You need to store the index for the question you display on screen then on answer submission, you can use remove at index method as: **questionList.remove(at: index)** – Rahul Kumar Jul 23 '17 at 17:18
0

Ypu can use the folleowing code to maintain a collection of buttons and connect mutiples of them with tis array from your storyboard or nib:

@IBOutlet var collectionOfButtons: Array<UIButton>?

Here you can find a more detailed example.

moglistree
  • 61
  • 2