I am writing code for a iOS quiz app and so far the code contains a dictionary that has the question as the key and the answers for that question as it values. My code randomizes an array of the keys (questions) and selects a key to be displayed at random. I also put the values (answers) in an array of values and randomized them as well and made sure that whatever index of of the array of questions is selected the same index of the array of answers is chosen.
However when I run the simulator the question label text remains the same and doesn't select a new index from the array of questions to be chosen as a new displayed question but on the other hand the array of answers to change after I select an answer.
Simply put, the question label text always stays the same and the title for the buttons (array of values) do select a different index. How can I make it to where my question label changes and doesn't stay the same?
Here is 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)))
let randAnswers = rand
//random answer index
var answerList = Array(listQuestions.QADictionary.values)
var choices = answerList[randAnswers]
//fetch questions from list
var question = questionList[rand]
questionLabel.text = question
//function for new question and button titles
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()
}
}
//variables
var currentQuestion = 0
var rightAnswerBox:UInt32 = 0
var index = 0
//Question Label
@IBOutlet weak var questionLabel: UILabel!
//Answer Button
@IBAction func buttonAction(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerBox)) {
//removes asked question
print ("Correct!")
}
else {
wrongSeg()
print ("Wrong!")
}
}
override func viewDidAppear(_ animated: Bool) { questionWithAnswers()
}
}
Aucun commentaire:
Enregistrer un commentaire