2014-10-10 89 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 
}) 
相關問題