无法将 'UIView' 类型的值转换为 'SCNView'

Could not cast value of type 'UIView' to 'SCNView'

应用程序构建良好,直到我开始向故事板添加按钮然后我收到上述错误。它在我的 GameViewController 中的这一行代码处停止。

let scnView = self.view as! SCNView

故事板本身的自定义 class 设置为 GameViewController(SCNView 没有选项)。 GameViewController 本身继承自 UIViewController。 我正在使用 Xcode7.2.

确保在文件顶部也导入 SceneKit。

import SceneKit

打开“Main.storyboard”文件。 Select ViewController -> 查看转到“Identity Inspector”并将 Class 字段更改为 SCNView。 SceneKit Help

如果您在没有情节提要的情况下以编程方式设置所有这些内容,请确保执行以下步骤:

  1. 常规 -> 部署信息 -> 清除主界面部分,留空。
  2. 转到AppDelegate.swift并分配window
  3. 的rootViewController

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow()
    let gameViewController = GameViewController()
    window?.rootViewController = gameViewController
    window?.makeKeyAndVisible()
    return true
}

  1. 转到GameViewController,这里不要将GameViewController的视图投射到SCNView,而是创建SCNView实例并添加类似subView:

let scnView = SCNView(frame: view.frame)
view.addSubview(scnView)

当我删除一个 UIButton 时,同样的事情发生在我身上,这也删除了 UIView。所以添加一个UIView,把class改成SCNView

如果您以编程方式执行此操作,请添加以下代码:

    override func loadView() {
        //sort of like changes type of self.view from UIView to SCNView
        self.view = SCNView()
    }