简体   繁体   English

UIViewController不会选择另一个UIViewController吗?

[英]UIViewController will not segue to another UIViewController?

I have my GameViewController that contains a SCNView. 我有包含SCNView的GameViewController。 I can segue to my GameViewController but when my player dies I try to segue to my GameOverVC but it does not segue. 我可以选择我的GameViewController,但是当玩家死亡时,我尝试选择我的GameOverVC,但不能选择。 I know the code is being called because I print something to the console. 我知道正在调用代码,因为我在控制台上打印了一些东西。 The segue is setup in the storyboard. 在情节提要中设置了segue。

在此处输入图片说明

I segue by calling 我打电话打电话

self.performSegue(withIdentifier: "gameOver", sender: self)

I have done this in multiple apps but I can not figure out why this is not working. 我已经在多个应用程序中做到了这一点,但我不知道为什么这不起作用。 This is my first app with SceneKit so I don't know if that changes anything but any help is appreciated! 这是我的第一个带有SceneKit的应用程序,所以我不知道它是否会发生任何变化,但是可以帮助您!

Also

If I try 如果我尝试

let vc = self.view.window?.rootViewController

vc?.present(GameOverVC(), animated: true, completion: nil)

Then it does present the UIViewController but it does not show anything I have added in that view? 然后,它确实显示了UIViewController,但没有显示我在该视图中添加的任何内容?

segue works for UIVIewControllers not for SpriteKit scenes . segue适用于UIVIewControllers不适用于SpriteKit scenes So, you need to perform segue from parent viewcontroller , so you can do something like, 因此,您需要从parent viewcontroller执行segue,以便可以执行以下操作:

 self.parentViewController.performSegue(withIdentifier: "gameOver", sender: self)

Or as mentioned by @ZeMoon in this answer , you can make property of your viewcontroller in SKScene class and you can perform segue over this property something like , 或如@ZeMoon在此答案中所提到的,您可以在SKScene类中设置viewcontroller属性,并可以对该属性执行segue,例如,

 class GameScene: SKScene {
    var viewController: UIViewController?
    ...
 } 

Then, in the viewController class, just before skView.presentScene(scene) do, 然后,在viewController类中,在skView.presentScene(scene)之前,

  scene.viewController = self

Now you can perform segue like, 现在您可以像

  self.viewController.performSegueWithIdentifier("gameOver", sender: self.viewController)

OR 要么

Forget segue just instantiate your GameOverVC and push it on your navigation controller like, 忘了segue只需实例化您的GameOverVC并将其推GameOverVC导航控制器上,例如,

    let nav = self.view.window?.rootViewController as! UINavigationController

    nav.pushViewController(gameOverVC, animated: true)

Or 要么

   self.navigationController?.pushViewController(gameOverVC, animated: true)

Somehow I had the same problem and asynchronous-call rescue me: I did something like this: 不知何故,我遇到了同样的问题,异步呼叫救了我:我做了这样的事情:

 DispatchQueue.main.asyncAfter(deadline: .now()) {

        self.performSegue(withIdentifier: "gameOver", sender: self)
    }

if you need to segue from an object that is not a ViewController inherited class, you need to pass a ref of the parentViewController to your object and make the call like this: 如果您需要从不是ViewController继承类的对象中查询,则需要将parentViewController的ref传递给您的对象并进行如下调用:

DispatchQueue.main.asyncAfter(deadline: .now()) {

        parentViewController.performSegue(withIdentifier: "gameOver", sender: self)
    }

You can try following :- 您可以尝试以下:

UIViewController *vc = self.view.window.rootViewController;
[vc performSegueWithIdentifier:@"id" sender:nil];

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

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