2014-10-10 87 views
1

我正在寻找使用SpriteKit从gameScene更改我的ViewController。 我有3个不同的ViewControllers场景。如何在SKScene中的几个ViewController之间进行转换?

当我的应用程序以PresentationViewController开头时(参见截图1),我从GameScene(GameViewController)到GameOverViewController的转换代码不起作用,我可以读取一条错误消息。 screenshot1

错误消息:

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)过渡代码完美。

screenshot2

我不知道为什么会出现这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) 
} 

回答

2

好吧,我发现自己的方式几个ViewController之间进行导航。 我设置secondViewController是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 
}) 
相关问题