15

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?

Michel Kansou
  • 2,851
  • 4
  • 13
  • 22

2 Answers2

42

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.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • it doesn't work it give an error "Getter for 'presentingViewController' with Objective-C selector 'presentingViewController' conflicts with getter for 'presentingViewController' from superclass 'UIViewController' with the same Objective-C selector" i updated my post so you can see my code for GameOverView – Michel Kansou Aug 11 '15 at 23:21
  • First of all, `var presentingViewController :UIViewController! = self.presentViewController` outside a function doesn't work , you need to declare it and then inside your `viewDidLoad` instantiate it. Your problem is regarding that when you declare `presentingViewController` as a stored property the `UIViewController` make conflict with his own `presentingViewController`, solution: change the name. The above code is for be called inside a function to avoid this kind of conflicts. – Victor Sigler Aug 11 '15 at 23:30
  • Thanks for the help it return to MainMenu but the GameViewController isn't dismiss yet the memory wasn't released – Michel Kansou Aug 11 '15 at 23:36
  • What do you mean with "isn't dismiss yet the memory wasn't released"? – Victor Sigler Aug 11 '15 at 23:37
  • When i dismiss from GameViewController the memory decrease from 60MB to 40MB but if i dismiss from GameOverView the memory stay at 60MB and if i play again the game the memory increase from 60 to 100 MB – Michel Kansou Aug 11 '15 at 23:39
  • Then I recommend you to use Instruments and find where is the bug memory. – Victor Sigler Aug 11 '15 at 23:53
  • It work find now thanks but is it normal that every time i start the game this error is prompt ": calling -display has no effect." i don't use CAMetalLayer in my code how do i solve it thx – Michel Kansou Aug 12 '15 at 06:43
3

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)
Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36
Abhinav Jha
  • 295
  • 1
  • 17