These questions come from an answer I got from another question here.
I wonder does the function calls commented below matter? I tried to run the app and close the alert, but dismissViewControllerAnimated is never being called.
Could someone share some experience on when/how to use such function?
class GameViewController: UIViewController {
@IBOutlet var gameBoardUIView: GameBoardUIView
...
}
class GameBoardUIView: UIView {
...
func move() {
if !gameBoard.checkNextMoveExist() {
let parentViewController: UIViewController = UIApplication.sharedApplication().windows[1].rootViewController
var alert = UIAlertController(title: "Game Over", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Take Me Back", style: UIAlertActionStyle.Cancel, handler: {(action: UIAlertAction!) in
// parentViewController.dismissViewControllerAnimated(true, completion: nil)
println("Taking user back to the game without restarting")
}))
alert.addAction(UIAlertAction(title: "New Game", style: UIAlertActionStyle.Destructive, handler: {(action: UIAlertAction!) in
// parentViewController.dismissViewControllerAnimated(true, completion: nil)
println("Starting a new game")
self.restartGame()
}))
parentViewController.presentViewController(alert, animated: true, completion: nil)
}
}
}
Edit: One more specific question to the example I gave above. Why dismissViewControllerAnimated is not doing anything when I click the button on the alert? The messages(e.g. "Starting a new game") are printed successfully. Is it because I get the controller from the UIWindow array?