我正在寻找使用SpriteKit从gameScene更改ViewController。
我有3个场景以及3个不同的ViewController。

当我的应用程序以PresentationViewController开头(请参见屏幕快照1)时,我从GameScene(GameViewController)到GameOverViewController的过渡代码不起作用,并且我可以读取错误消息。

错误信息 :

whose view is not in the window hierarchy!

我的代码是:
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let settingController: UIViewController = storyboard.instantiateViewControllerWithIdentifier("PresentationViewController") as UIViewController
    let vc = self.view?.window?.rootViewController
    vc?.presentViewController(settingController, animated: true, completion: nil)

当我的应用程序以GameViewController开头(请参见屏幕截图2)时,过渡代码可以完美运行。

我不知道为什么这两种情况有区别。

有关信息,PresentationViewController和GameViewController之间的过渡是使用带有以下代码的UIButton:
override func viewDidLoad() {

    super.viewDidLoad()

    var playButton   = UIButton.buttonWithType(UIButtonType.System) as UIButton
    let image = UIImage(named: "playButton.png") as UIImage

    playButton.frame = CGRectMake(0, 0, 100, 100)
    playButton.center = CGPointMake(self.view.frame.width/2, self.view.frame.height/1.7)
    playButton.addTarget(self, action: "transition:", forControlEvents: UIControlEvents.TouchUpInside)
    playButton.setBackgroundImage(image, forState: UIControlState.Normal)

    self.view.addSubview(playButton)

}

func transition(sender:UIButton!)
{

    println("transition")
    let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("GameViewController") as UIViewController

    secondViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
    self.presentViewController(secondViewController, animated: true, completion: nil)
}

最佳答案

好吧,我自己找到了一种在几个ViewController之间导航的方法。
我将第二个ViewController设置为rootViewController,以替换PresentationViewController文件func transition()中的此代码:

self.presentViewController(secondViewController, animated: true, completion: nil)`

通过此代码(带有选项):
secondViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
let window = UIApplication.sharedApplication().windows[0] as UIWindow
UIView.transitionFromView(
    window.rootViewController!.view,
    toView: secondViewController.view,
    duration: 0.65,
    options: .TransitionCrossDissolve,
    completion: {
        finished in window.rootViewController = secondViewController
})

关于ios - 如何在SKScene的多个ViewController之间进行转换?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26308021/

10-09 16:13