Ios 如何在场景中的多个ViewController之间进行转换?
我正在寻找用SpriteKit从游戏场景更改我的ViewController。 我有3个场景和3个不同的视图控制器 当我的应用程序以PresentationViewController(请参见屏幕截图1)启动时,我从游戏场景(GameViewController)到GameOverViewController的转换代码不起作用,我可以读取错误消息。 错误消息:Ios 如何在场景中的多个ViewController之间进行转换?,ios,swift,sprite-kit,viewcontroller,skscene,Ios,Swift,Sprite Kit,Viewcontroller,Skscene,我正在寻找用SpriteKit从游戏场景更改我的ViewController。 我有3个场景和3个不同的视图控制器 当我的应用程序以PresentationViewController(请参见屏幕截图1)启动时,我从游戏场景(GameViewController)到GameOverViewController的转换代码不起作用,我可以读取错误消息。 错误消息: whose view is not in the window hierarchy! 我的代码是: let storyboa
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之间的转换使用带有以下代码的UI按钮:
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
之间导航的方法。
我将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
})