简体   繁体   English

如何从GameScene.swift中关闭ViewController?

[英]How can I dismiss a ViewController from my GameScene.swift?

QUESTION: How can I dismiss a ViewController from my GameScene.swift ? 问题:如何从GameScene.swift中关闭ViewController?


SITUTATION: I have 2 VCs in my SpriteKit Game, like so: 情况:我的SpriteKit游戏中有2个VC,如下所示:

ViewController.swift ----Press Play-----> GameViewController ViewController.swift ----按Play -----> GameViewController

When the player loses, I want to dismiss the GameViewController so the player can press play again. 当玩家失败时,我想解散GameViewController,以便玩家可以再次按下play。 I check for the player's loss in my GameScene.swift and would like to dismiss the GameVC from there. 我在GameScene.swift中检查玩家的损失,并希望从那里解散GameVC。


NB: Googled this without success. 注意:用谷歌搜索没有成功。


WHAT I TRIED: 我尝试过的是:

1) Creating a gameVC instance in my GameScene.swift and dismissing it like so: 1)在我的GameScene.swift中创建一个gameVC实例并将其关闭,如下所示:

let gameVC = GameViewController()
gameVC.dismissViewController(false,completion: nil)

2) Doing: 2)做:

self.view.window!.rootViewController?.dismissViewControllerAnimated(false, completion: nil)

Those don't work for obvious reasons ^^ 那些由于明显原因而无法工作^^

You don't want to "grab" the existing instance: https://pragprog.com/articles/tell-dont-ask 您不想“抢”现有实例: https : //pragprog.com/articles/tell-dont-ask

You need to either hand GameScene a reference to the view controller so it can dismiss it, or use the delegate pattern to communicate backwards to a controlling object that the VC should be dismissed/dismiss itself. 您需要将GameScene交给视图控制器的引用,以便可以将其关闭,也可以使用委托模式向后传递给控制对象,以使VC自行关闭/关闭。

A simple example… you can add a GameViewController property to GameScene , then dismiss the VC at the appropriate time: 一个简单的例子……您可以将GameViewController属性添加到GameScene ,然后在适当的时间关闭VC:

class GameScene: SKScene {
    var gameVC: GameViewController?

    func gameDidEnd() {
        gameVC?.dismissViewControllerAnimated(true) {
            // if desired, do any cleanup after the VC is dismissed
        }
    }
}

Then, just set this property when creating the GameScene object in the first place: 然后,首先在创建GameScene对象时设置此属性:

if let gameScene = GameScene(fileNamed: "MyScene") {
    gameScene.gameVC = someGameVC
}

This simple approach will tightly couple GameScene and GameViewController , making it a bit more difficult if you ever want to use one of these objects without the other. 这种简单的方法将把GameSceneGameViewController紧密地结合在一起,如果您想不使用其他对象就很难使用它们。 But for this simple use case, it may be fine. 但是对于这个简单的用例,可能很好。

I've follow some of your discussion. 我已经关注了您的一些讨论。 I want to add some code, because usually I prefeer to work with one ViewController or two and many SKScene and SKNode, but in this case could be useful to have a currentViewController reference: 我想添加一些代码,因为通常我倾向于使用一个或两个ViewController和许多SKScene和SKNode,但是在这种情况下,使用currentViewController参考可能会很有用:

class MyModelScene: SKScene {
   let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
   var currentViewController : MyModelViewController! = MyModelViewController() 
 // MyModelViewController is a customized UIViewController

   override func didMoveToView(view: SKView) {
      super.didMoveToView(view)
      print("---")
      print("∙ \(NSStringFromClass(self.dynamicType))")
      print("---")
   }
}

class Level1Scene: MyModelScene {
    ...
}

In the UIViewController : UIViewController

class PreloadViewController: MyModelViewController {
     override func viewDidLoad() {
        super.viewDidLoad()

        if let scene = Level1Scene(fileNamed:"Level1Scene") {
            // Configure the view.
            let skView = self.view as! SKView
            skView.showsFPS = true
            skView.showsPhysics = true
            skView.showsNodeCount = true

            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .ResizeFill 
            scene.currentViewController = self
            skView.presentScene(scene)
        }
    }
}

With this code , you've always a currentViewController reference in your SKScene and you can check if it's the correct viewController you want to dismiss or not. 使用此代码,您在SKScene始终具有currentViewController引用,并且可以检查它是否是您要关闭的正确viewController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM