0

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:

multiple choice quiz screen shot

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jfulton
  • 77
  • 1
  • 10

1 Answers1

2

You set first index of answerChoices array for all options with AnswerQuestions initially = 0 , instead you need

optionA.setTitle(surveyQuestions?.questions[questionNumbers].answerChoices[0].text, for: .normal)
optionB.setTitle(surveyQuestions?.questions[questionNumbers].answerChoices[1].text, for: .normal)
optionC.setTitle(surveyQuestions?.questions[questionNumbers].answerChoices[2].text, for: .normal)
optionD.setTitle(surveyQuestions?.questions[questionNumbers].answerChoices[3].text, for: .normal)
optionE.setTitle(surveyQuestions?.questions[questionNumbers].answerChoices[4].text, for: .normal)

It'll be easier if you have created an IBOutlet collection

@IBOutlet var allOptions: [UIButton]!

and hooked all buttons to it , in this case you can do

allOptions.indices.forEach { allOptions[$0].setTitle(surveyQuestions?.questions[questionNumbers].answerChoices[$0].text, for:.normal)  }
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • It’s not a good idea to depend on the order of the items in an outlet collection. Perhaps you should use the tag. See https://stackoverflow.com/q/15189922/1630618 – vacawama Jun 20 '19 at 00:27
  • @Sh_Khan thanks I cant believe it was something as simple as this. Thanks for helping out. And yes I am using the tag method. – jfulton Jun 20 '19 at 16:12
  • @Sh_Khan I am running into another problem. When I parse the data into my question text label I am obviously getting the text to come through, but there are also image links within the text. Ex: "https://website.com/imagepath/imagename.png". How do I parse the url string and apply it to a UIImage? – jfulton Aug 27 '19 at 21:16