-1

I'm trying to make an app where if the score is 3 the app displays a message that says "you lose" but keeps '3' as the number in the score label until the End Game option in the popup is pressed, at which point the score goes back to 0 for a new game. I am new to swift and am having difficulty and would really appreciate any and all help! I am not sure if making an IBAction for the alert action is the right thing to do or not.

    else if rightscorecount == 3 {
        let alert = UIAlertController(title: "Game", message: "You Lose!", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "End Game", style: UIAlertActionStyle.default) { UIAlertAction in})
       self.present(alert, animated: true, completion: nil)

    }

}
@IBAction func test(sender: UIAlertAction) {

    rightscorecount = 0
    rightscorelabel.text = String(rightscorecount)

}
radaro73
  • 11
  • 1
  • 1
    There is a completion block when presenting `UIAlertController`. You can add the logic there. – Raptor May 11 '17 at 01:57

1 Answers1

2

Try this:

let alertController = UIAlertController.init(title: "Game", message: "You Lose!", preferredStyle: .alert)
    alertController.addAction(UIAlertAction.init(title: "End Game", style: .default, handler: { (action) in
        // Your handler goes here
        self.someFunction()
    }))
    self.present(alertController, animated: true) { 
        // Completion block
    }

And your function

func someFunction() {
    // Function body goes here
}
Mannopson
  • 2,634
  • 1
  • 16
  • 32