Skip to content

Instantly share code, notes, and snippets.

@KrauserHuang
Created January 9, 2021 04:31
Show Gist options
  • Save KrauserHuang/93a3e8a6294d536dc0e6eefb2fdeefa9 to your computer and use it in GitHub Desktop.
Save KrauserHuang/93a3e8a6294d536dc0e6eefb2fdeefa9 to your computer and use it in GitHub Desktop.
@IBAction func chooseAnswer(_ sender: UIButton) {
let selectedAnswer = sender.currentTitle!
// switch sender.tag {
// case 1:
// selectedAnswer = newQandA[questionIndex].optionA
// case 2:
// selectedAnswer = newQandA[questionIndex].optionB
// case 3:
// selectedAnswer = newQandA[questionIndex].optionC
// case 4:
// selectedAnswer = newQandA[questionIndex].optionD
// default:
// break
// }
if checkAnswer(userAnswer: selectedAnswer) {
sender.backgroundColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
} else {
sender.backgroundColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
}
nextQuestion()
Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(showQandA), userInfo: nil, repeats: false)
}
func checkAnswer(userAnswer: String) -> Bool {
if userAnswer == newQandA[questionIndex].answer {
scoreNumber += 1
return true
} else {
return false
}
}
func nextQuestion() {
if questionIndex + 1 < newQandA.count {
questionIndex += 1
} else {
performSegue(withIdentifier: "showResult", sender: ResultViewController.self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment