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

爷,独闯天下 提交于 2019-12-12 09:55:06

问题


App was building fine until I started adding buttons to the storyboard then I got the above error. It stops at this line of code inside my GameViewController.

let scnView = self.view as! SCNView

The storyboard itself has its custom class set to GameViewController (theres no option for SCNView). GameViewController itself inherits from UIViewController. Im using Xcode7.2.


回答1:


Make sure to also import SceneKit at the top of the file.

import SceneKit

Open the “Main.storyboard” File. Select ViewController -> View go to “Identity Inspector” and change the Class field to SCNView. SceneKit Help




回答2:


If you are setting all these stuff programmatically without storyboard, make sure to do these steps:

  1. General -> Deployment Info -> Clear the Main Interface section, leave empty.
  2. Go to AppDelegate.swift and assign rootViewController of window

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow()
    let gameViewController = GameViewController()
    window?.rootViewController = gameViewController
    window?.makeKeyAndVisible()
    return true
}
  1. Go to GameViewController and here don't cast GameViewController's view to SCNView, instead create SCNView instance and add like subView:

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



回答3:


Same thing happened to me when I deleted a UIButton, which also deleted the UIView. So add a UIView and change class to SCNView



来源:https://stackoverflow.com/questions/35323147/could-not-cast-value-of-type-uiview-to-scnview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!