UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 21 - Project 2 challenges

Forums > 100 Days of Swift

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)
        }
    }

3      

You are trying to show two alerts at once when player answers 10 questions. This is not supported because the first alert already "occupies" the screen. The standard way to present different content in alert is to first configure title and optionally message variables (in this case based on number of answers) and then present the alert with those variables passed in.

3      

What @nemecek-filip or

if questionsAnswered < 10 {
   var ac = UIAlertController(title: title, message: "Your score is \(score).", preferredStyle: .alert)
   ac.addAction(UIAlertAction(title: "Play again", style: .default, handler: askQuestion))
   present(ac, animated: true)
} else {
  var 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)
  score = 0
}

but a @nemecek-filip is proberbly better but both only present one Alert

3      

Thank you both! This makes a lot more sense now. I'll give that a try and see what happens.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.