0

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?

Community
  • 1
  • 1
Bing
  • 181
  • 1
  • 3
  • 12

1 Answers1

0

It basically dismisses the current ViewController on the stack and shows the previous ViewController that it came from. This method is used when you want to go back to the previous View Controller from where you came.

EDIT :

The reason probably, why its not doing anything is because you will have to use some method or delegate on handling the AlertView Button click events. Otherwise it will function as a plain blank click and stay where it is. I don't know the method in Swift, but in Xcode there's a method called :

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //your code for alert button click
}

This handles what you want to perform on Alert Button clicks.

Hope this helps.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34
  • Thanks for the quick help! I think I understand much better now. It basically looks like a go-back-to-previous-page function. Could you also share some ideas on my new question regarding UIWindow? – Bing Jul 05 '14 at 09:57
  • See my updated answer. That might help you regarding alert button. – Dhrumil Jul 05 '14 at 10:02
  • Actually I was using it this way. But UIAlertView is deprecated in swift and I'm trying to use UIAlertController as suggested. The button actions I added work as expected. I could see the game is restarted when I click the Start New Game button. But the dismiss function is somehow not doing anything. – Bing Jul 05 '14 at 10:05
  • Check your controller name. It should be the name of the current controller that you are on and not the controller that you want to move back to. – Dhrumil Jul 05 '14 at 10:10
  • How do I check the name of controller during runtime? I could confirm that the controller is the right one because the alert is presented successfully in the correct view. But only the dismiss func won't work when I press the alert action buttons. – Bing Jul 05 '14 at 23:56