Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss the current ViewController and go to another View in Swift

I am new to Swift and I want to know how to dismiss the current view controller and go to another view.

My storyboard is like the following: MainMenuView -> GameViewController -> GameOverView. I want to dismiss the GameViewController to go to the GameOverView, not to the MainMenuView.

I use the following code in my MainMenuView:

@IBAction func StartButton(sender: UIButton) {
    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("GameViewController") as! GameViewController
    self.presentViewController(nextViewController, animated:true, completion:nil)
    restGame()
}

In the GameViewController, I use this code, but it doesn't dismiss the GameViewController.

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("GameOverView") as! GameOverView
self.presentViewController(nextViewController, animated:true, completion:nil)

This is My GameOverView Code :

class GameOverView: UIViewController{
    // save the presenting ViewController
    var presentingViewController :UIViewController! = self.presentViewController

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func ReplayButton(sender: UIButton) {
        restGame()
        didPressClose()
    }
    @IBAction func ReturnMainMenu(sender: UIButton) {
        Data.GameStarted = 1
        self.dismissViewControllerAnimated(false) {
            // go back to MainMenuView as the eyes of the user
            self.presentingViewController.dismissViewControllerAnimated(false, completion: nil);
        }
       /* let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("MainScene") as! MainScene
        self.presentViewController(nextViewController, animated:true, completion:nil)*/

    }
    func restGame(){
        Data.score = 0
        Data.GameHolder = 3
        Data.GameStarted = 1
        Data.PlayerLife = 3.0
        Data.BonusHolder = 30
        Data.BonusTimer = 0
    }
    func didPressClose()
    {
        self.self.dismissViewControllerAnimated(true, completion:nil)
    }
    override func shouldAutorotate() -> Bool {
        return false
    }

    deinit{
        print("GameOverView is being deInitialized.");

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }


}

Any suggestions?

like image 705
Michel Kansou Avatar asked Aug 11 '15 22:08

Michel Kansou


People also ask

How to present/dismiss UIViewController’s view in Swift example?

How To Present / Dismiss UIViewController’s View In Swift Example. 1 There is a yellow background button with blue text in the main UIViewController view. 2 When you click the button, it will present the second UIViewController view. 3 The second UIView window has red background color, and there is a Dismiss This View button in it. More items...

How to send notifications to multiple view controllers in Swift?

To handle the notification when it gets sent, you should implement a method and use it as the selector parameter for the addObserver implemented above: Finally, to send the notification from another View Controller, just use the post method with your notification name: So, these were five ways of passing data between View Controllers in Swift.

How do I show two view controllers in one view controller?

Select the “Show” option from the “Action Segue” menu. Control + click the button and drag to the second ViewController, then select “Show.” Now, on the first ViewController class, you need to override the prepare (for segue) method: 2. Delegate Design Pattern

How do I dismiss the second UIView window?

The second UIView window has a red background color, and there is a Dismiss This View button in it. When you click the Dismiss This View button, it will dismiss the second UIView and return back to the main UIView.


2 Answers

What you can do is let the GameOverView be presented, after all when you presenting it the GameViewController is below in the hierarchy, and then in your GameOverView run the following code to close both when you want to dismiss the GameOverView, like in the following way:

@IBAction func ReturnMainMenu(sender: UIButton) {
    // save the presenting ViewController
    var presentingViewController: UIViewController! = self.presentingViewController

    self.dismissViewControllerAnimated(false) {
          // go back to MainMenuView as the eyes of the user
          presentingViewController.dismissViewControllerAnimated(false, completion: nil)
    }
}

The above code need to be called when you want to dismiss the GameOverView.

I hope this help you.

like image 162
Victor Sigler Avatar answered Sep 28 '22 03:09

Victor Sigler


The below code will take you to the main VC, Here's a tried and tested piece of code.

self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
like image 29
Abhinav Jha Avatar answered Sep 28 '22 05:09

Abhinav Jha