I am developing a multiple choice quiz. All of the data is being parsed through json. I have 5 questions and each question has 5 multiple choice answers. Currently I set the question number, title of the question with description, and have the 5 buttons with each question.
It is working with one exception. Each of the buttons is displaying the same question instead of each individual question. I assume that I need to create a loop that loops through the question and appends it to each button, but am having trouble figuring this out.
Here is an example of what I see:
Here is the code I have written:
@IBOutlet weak var progressView: UIView!
@IBOutlet weak var questionCounter: UILabel!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var questionNumber: UILabel!
@IBOutlet weak var questionTitle: UILabel!
@IBOutlet weak var questionText: UILabel!
@IBOutlet weak var optionA: UIButton!
@IBOutlet weak var optionB: UIButton!
@IBOutlet weak var optionC: UIButton!
@IBOutlet weak var optionD: UIButton!
@IBOutlet weak var optionE: UIButton!
//Below is for parsed json data version
var allQuestions = [Questions]()
var questionNumbers: Int = 0
var AnswerQuestions: Int = 0
var selectedAnswer: Int = 0
func updateUI() {
self.questionNumber.text = String(surveyQuestions?.questions[questionNumbers].questionNumber ?? 0)
self.questionTitle.text = surveyQuestions?.questions[questionNumbers].category.name
self.questionText.text = surveyQuestions?.questions[questionNumbers].text
self.questionText.text = surveyQuestions?.questions[questionNumbers].text.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
optionA.setTitle(surveyQuestions?.questions[questionNumbers].answerChoices[AnswerQuestions].text, for: UIControl.State.normal)
optionB.setTitle(surveyQuestions?.questions[questionNumbers].answerChoices[AnswerQuestions].text, for: UIControl.State.normal)
optionC.setTitle(surveyQuestions?.questions[questionNumbers].answerChoices[AnswerQuestions].text, for: UIControl.State.normal)
optionD.setTitle(surveyQuestions?.questions[questionNumbers].answerChoices[AnswerQuestions].text, for: UIControl.State.normal)
optionE.setTitle(surveyQuestions?.questions[questionNumbers].answerChoices[AnswerQuestions].text, for: UIControl.State.normal)
print("Success")
}
From what I have gathered there are a few ways to possibly do this.
for button in question {
button.setTitle(question?.questions[number].text, for: UIControl.State.normal)
}
or
for i in 0...<button.count {
button.setTitle(question?.questions[number].text, for: UIControl.State.normal)
}
If anyone can help me out that would be awesome.
Thanks and if you need more details or info I can provide them.