How do I change the alert controller message when the player has answered 10 questions?
My solution so far was to create a questionsAnswered variable and increment it each time a button is tapped. That part works.
I also changed ac to be declared with var instead of let, and then set up and if statement that shows a alert controller with a different message when questionsAnswered == 10. But... it seems to be in the wrong place because when I try to run it I can play the game through 10 questions, but then I get an error message in Xcode and the alert controller never pops up.
Is this the correct way to do it? Where do I need to put the if statement so that it will run correctly?
@IBAction func buttonTapped(_ sender: UIButton) {
var title: String
if sender.tag == correctAnswer {
title = "Correct"
score += 1
} else {
title = """
Wrong
That was \(countries[sender.tag].uppercased()).
"""
score -= 1
}
questionsAnswered += 1
var ac = UIAlertController(title: title, message: "Your score is \(score).", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Continue", style: .default, handler: askQuestion))
present(ac, animated: true)
if questionsAnswered == 10 {
ac = UIAlertController(title: title, message: "Your final score is \(score).", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Play again", style: .default, handler: askQuestion))
present(ac, animated: true)
}
}