如何解除当前的ViewController并转到Swift中的另一个View [英] How to dismiss the current ViewController and go to another View in Swift

查看:78
本文介绍了如何解除当前的ViewController并转到Swift中的另一个View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的故事板如下所示:MainMenuView - > GameViewController - > GameOverView。我想解雇GameViewController转到GameOverView,而不是MainMenuView。

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

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

在GameViewController中,我使用此代码,但它不会解雇GameViewController。

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)

这是我的GameOverView代码:

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
    }


}

有什么建议吗?

推荐答案

你可以做的是让 GameOverView 出现,毕竟当你呈现它时, GameViewController 在层次结构中,然后在你的 GameOverView 运行以下代码,以便在您想要关闭 GameOverView 时关闭它们,如下所示:

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

当你要解雇时,需要调用上面的代码 GameOverView

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

我希望这可以帮到你。

这篇关于如何解除当前的ViewController并转到Swift中的另一个View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆