我有一个UITabBarController有两个项目,第一个是日历,它只是一个UIWebView 。 第二个项目带来一个SpriteKit游戏场景并隐藏标签栏(游戏需要全屏)。
我有一个SKSpriteNode图像,当点击时,我希望视图更改回日历视图(第一个标签栏项)。 我已经找到了一种方法来做这个,并尝试了我在StackOverflow上找到的各种建议,但没有任何工作。 该应用程序因错误而崩溃
“打开一个可选项时出乎意料地发现了nil”
我明白这个错误意味着什么,而不是为什么我得到它。 这是相关的代码(我试图弄清楚这一点时从这里得到的部分代码):
在GameScene.swift中:
weak var viewController: GameViewController! override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { let touch:UITouch = touches.first! as UITouch let positionInScene = touch.locationInNode(self) let touchedNode = self.nodeAtPoint(positionInScene) if let name = touchedNode.name { if name == "exit" { self.viewController.gameOver() self.removeFromParent() self.view?.presentScene(nil) } } }在GameViewController.swift中:
var currentGame: GameScene! override func viewDidLoad() { super.viewDidLoad() self.tabBarController?.tabBar.hidden = true if let scene = GameScene(fileNamed:"GameScene") { // Configure the view. let skView = self.view as! SKView skView.showsFPS = false skView.showsNodeCount = false skView.ignoresSiblingOrder = true scene.scaleMode = .AspectFill scene.viewController = self skView.presentScene(scene) currentGame = scene } } func gameOver() { performSegueWithIdentifier("backToMain", sender: nil) }这是给出错误的行:
self.viewController.gameOver()I have a UITabBarController that has two items, the first one being a calendar which is just a UIWebView. The second item brings in a SpriteKit game scene and hides the tab bar (The game needs to be full screen).
I have an SKSpriteNode image that when tapped, I'd like the view to change back to the calendar view (the first tab bar item). I've searched for a way to do this and have tried various suggestions I've found here on StackOverflow, but nothing is working. The app crashes with the error
"Unexpectedly found nil while unwrapping an optional"
I understand what that error means, just not why I'm getting it. Here is the relevant code (part of which I got from here while trying to figure this out):
In GameScene.swift:
weak var viewController: GameViewController! override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { let touch:UITouch = touches.first! as UITouch let positionInScene = touch.locationInNode(self) let touchedNode = self.nodeAtPoint(positionInScene) if let name = touchedNode.name { if name == "exit" { self.viewController.gameOver() self.removeFromParent() self.view?.presentScene(nil) } } }In GameViewController.swift:
var currentGame: GameScene! override func viewDidLoad() { super.viewDidLoad() self.tabBarController?.tabBar.hidden = true if let scene = GameScene(fileNamed:"GameScene") { // Configure the view. let skView = self.view as! SKView skView.showsFPS = false skView.showsNodeCount = false skView.ignoresSiblingOrder = true scene.scaleMode = .AspectFill scene.viewController = self skView.presentScene(scene) currentGame = scene } } func gameOver() { performSegueWithIdentifier("backToMain", sender: nil) }This is the line giving the error:
self.viewController.gameOver()最满意答案
你的代码是正确的; 我认为可能的问题是你在不传递viewController值的情况下呈现多个场景。 要关闭此问题,请使用“查找”导航器(Cmd + 3)搜索presentScene然后确保每次调用都是在将viewController属性的值传递给新场景之后。
Your code is correct; I think the likely problem is that you are presenting more than one scene without transferring the value of viewController. To track this problem down, use the Find navigator (Cmd+3) to search for presentScene then ensure each call is after you have transferred the value of the viewController property across to the new scene.
更多推荐
发布评论