小编典典

如何关闭当前的ViewController并转到Swift中的另一个视图

swift

我是Swift的新手,我想知道如何解散当前的视图控制器并转到另一个视图。

我的故事板如下:MainMenuView-> GameViewController->
GameOverView。我想关闭GameViewController转到GameOverView,而不是MainMenuView。

我在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()
}

在GameViewController中,我使用此代码,但不会关闭GameViewController。

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

这是我的GameOverView代码:

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
    }


}

有什么建议?


阅读 462

收藏
2020-07-07

共1个答案

小编典典

您可以做的就是GameOverView显示,毕竟,在显示它时,它GameViewController位于层次结构中的下面,然后在GameOverView运行时,当您要关闭时,下面的代码将两者都关闭GameOverView,就像这样:

@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)
    }
}

要关闭时需要调用上述代码GameOverView

希望对您有所帮助。

2020-07-07