gpt4 book ai didi

ios - 尝试使用 Storyboard ID 从 GameScene 移动到 ViewController 时发生 fatal error

转载 作者:行者123 更新时间:2023-11-30 13:58:07 25 4
gpt4 key购买 nike

主菜单 View (UIViewController)游戏玩法 (GameViewController.swift - UIViewController),我使用了以下代码,取得了巨大成功。 p>

我还测试了主菜单 View 游戏结束 View (UIViewController),以确保游戏结束 View 全部正确连接并且工作正常,但是我尝试从 GameScene.swift (SKScene) 调用一个函数到 GameViewController.swift,以在游戏结束时切换 View (游戏结束 View )。

但是我收到 fatal error :在解包可选值时意外发现 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)
}

如果您需要更多信息来帮助,请告诉我。

感谢大家的帮助!

<小时/>

更新代码

我的 Storyboard被命名为Main.storyboard

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

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)
}
<小时/>

更新以显示 Storyboard设置。

是否会因为我的游戏场景 Controller (UIViewControllers) 进入 SKScene 而导致导航 Controller 损坏而没有响应?奇怪的是我没有收到任何错误......

This is my pages basic storyboard setup

左上角:导航 Controller

顶部中心:主手动 Controller

右上角:游戏场景 Controller

底部中心:游戏结束 Controller

<小时/>

更新提供 GameViewController.swift

这是我的游戏 View Controller ,我没有分配导航 Controller ,因为我似乎不需要在主菜单 Controller 或游戏结束 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
}
}

最佳答案

我不知道你的 Storyboard是如何初始化的,但你可以在你的项目中指定你的 Storyboard

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)
}

应该可以

关于ios - 尝试使用 Storyboard ID 从 GameScene 移动到 ViewController 时发生 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33346977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com