尝试使用Storyboard ID从GameScene移动到ViewController时发生致命错误

时间:2015-10-26 13:27:40

标签: ios swift uiviewcontroller sprite-kit skscene

我已经使用以下代码,从主菜单视图(UIViewController)游戏玩法(GameViewController.swift - UIViewController)取得了巨大成功。

我还测试了主菜单视图游戏结束视图(UIViewController),以确保Game Over视图都正确连接并且工作正常但是我试过从 GameScene.swift(SKScene) GameViewController.swift 调用一个函数,以便在游戏结束时切换视图(Game Over view)。

但是我得到了一个致命错误:在打开一个可选值时意外地发现了nil

为什么我会通过GameViewController.swift尝试移出GameScene.swift而不是从主菜单或游戏结束的任何其他UIViewController中获取此错误?

func goToGameOver() {
        print("Fetched: goToGameOver()")
        let gameOverController = self.storyboard?.instantiateViewControllerWithIdentifier("GameOverController") as? GameOverController
        gameOverController!.missionMode = missionMode
        gameOverController!.missionLevel = missionLevel
        self.navigationController?.pushViewController(gameOverController!, animated: true)
    }

如果您需要更多信息,请告知我们。

感谢您的帮助!

更新了代码

我的故事板名为 Main.storyboard

我已根据代码和下面的答案进行了更新,正如您在评论中看到的那样,我不再收到任何错误,但我也没有看到从GameScene到GameOverController视图的视图中的更改/切换

func goToGameOver() {
        print("Fetched: goToGameOver()")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let gameOverController = storyboard.instantiateViewControllerWithIdentifier("GameOverController") as? GameOverController
        gameOverController!.missionMode = missionMode
        gameOverController!.missionLevel = missionLevel
        self.navigationController?.pushViewController(gameOverController!, animated: true)
    }

更新以显示故事板设置。

可能因为我的游戏场景控制器(UIViewControllers)进入SKScene并且现在导航控制器坏了而无法响应吗?我没有收到任何错误似乎很奇怪......

This is my pages basic storyboard setup

左上角:导航控制器

热门中心:主要手动控制器

右上角:游戏场景控制器

底部中心:游戏控制器

更新提供GameViewController.swift

这是我的游戏视图控制器,我还没有分配navigationController,因为我似乎不需要在我的主菜单控制器或Game Over Controller中执行此操作。如何在swift中分配navigationController?

import UIKit
import SpriteKit
import iAd

class GameViewController: UIViewController, ADBannerViewDelegate {

    var iAdBanner = ADBannerView()
    var bannerVisible = false

    var missionMode: Bool!
    var missionLevel: Int!

    override func viewDidLoad() {
        super.viewDidLoad()

        if let scene = GameScene(fileNamed:"GameScene") {
            // Configure the view.
            let skView = self.view as! SKView
            skView.showsFPS = true
            scene.size = skView.bounds.size
            skView.showsNodeCount = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .AspectFill

            scene.missionMode = missionMode
            scene.missionLevel = missionLevel

            skView.presentScene(scene)
        }
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        iAdBanner.frame = CGRectMake(0, -iAdBanner.frame.height, self.view.frame.width, 0)
        iAdBanner.delegate = self
        bannerVisible = false
    }

    func bannerViewDidLoadAd(banner: ADBannerView!) {
        if(bannerVisible == false) {

            // Add banner Ad to the view
            if(iAdBanner.superview == nil) {
                self.view.addSubview(iAdBanner)
            }

            // Move banner into visible screen frame:
            UIView.beginAnimations("iAdBannerShow", context: nil)
            banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height)
            banner.alpha = 1
            UIView.commitAnimations()
            bannerVisible = true
        }

    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        if(bannerVisible == true) {
            // Move banner below screen frame:
            UIView.beginAnimations("iAdBannerHide", context: nil)
            banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height)
            banner.alpha = 0
            UIView.commitAnimations()
            bannerVisible = false
        }

    }

    func goToGameOver() {
        print("Fetched: goToGameOver()")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        print(storyboard)
        let gameOverController = storyboard.instantiateViewControllerWithIdentifier("GameOverController") as! GameOverController
        gameOverController.missionMode = missionMode
        gameOverController.missionLevel = missionLevel
        print(gameOverController)
        self.navigationController?.pushViewController(gameOverController, animated: true)
    }

    override func shouldAutorotate() -> Bool {
        return true
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return .AllButUpsideDown
        } else {
            return .All
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

1 个答案:

答案 0 :(得分:0)

我不知道你的storyBoard init如何,但你可以用你的项目指定你的故事板

func goToGameOver() {
        print("Fetched: goToGameOver()")
        let storyboard = UIStoryboard(name: "YourStoryBoardName", bundle: nil)
        let gameOverController = storyboard.instantiateViewControllerWithIdentifier("GameOverController") as? GameOverController
        gameOverController!.missionMode = missionMode
        gameOverController!.missionLevel = missionLevel
        self.navigationController?.pushViewController(gameOverController!, animated: true)
    }

应该有效