[英]Swift Could not cast value of type 'UIView' to 'SKView'
First, I've reviewed the related questions: 首先,我回顾了相关问题:
None of those provided a clear answer for me, I'm unable to present a controller that has an SKView
in it from my main ViewController
, example: 这些都没有为我提供明确的答案,我无法从主
ViewController
中提供一个SKView
的控制器,例如:
import UIKit
import SceneKit
import SpriteKit
class ViewController: UIViewController {
var sceneView: SCNView!
var gameScene = GameScene()
var spriteScene: OverlayScene!
override func viewDidLoad() {
super.viewDidLoad()
sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
sceneView.scene = gameScene
}
func showModal() {
let popupViewController = PopupViewController()
popupViewController.modalPresentationStyle = .popover
present(popupViewController, animated: true, completion: nil)
}
}
But the issue seems to be when the PopupViewController
is called: 但是问题似乎出在调用
PopupViewController
时:
import UIKit
import SpriteKit
class PopupViewController:UIViewController {
var sceneView:SKView!
var spriteScene:PopupScene!
override func viewDidLoad() {
view.backgroundColor = UIColor.green
view.scene = spriteScene
super.viewDidLoad()
}
}
The error is raised: 引发错误:
Could not cast value of type 'UIView' (0x1b48c69c8) to 'SKView' (0x1b3b254b0).
Looking at your answer, you appear to be using the main view
of your PopupViewController
to add the scene to, rather than the new sceneView
that you declared. 查看您的答案,您似乎正在使用
PopupViewController
的主view
来添加场景,而不是使用您声明的新sceneView
。 The first way to fix this is, as said by Nathan Hitchings in the comments, to override loadView
and set self.view = sceneView
. 如Nathan Hitchings在评论中所述,解决此问题的第一种方法是覆盖
loadView
并设置self.view = sceneView
。 The other option is to open your storyboard or .xib and set the class type of the main view to be SKView
and then you can delete the sceneView
variable entirely and work with self.view
. 另一个选择是打开情节
SKView
或SKView
并将主视图的类类型设置为SKView
,然后可以完全删除sceneView
变量并使用self.view
。 Both essentially do the same thing, but in two different ways. 两者本质上都做相同的事情,但是以两种不同的方式。 Ensure that you give
sceneView
a value before using it too, as currently it appears to stay uninitialised. sceneView
确保在给sceneView
使用之前为其提供一个值,因为当前该值似乎尚未初始化。 Let me know if you need me to clarify anything. 让我知道您是否需要我澄清任何事情。
The first option would look something like this: 第一个选项如下所示:
override func loadView() {
sceneView = SKView()
sceneView.backgroundColor = .white
self.view = sceneView
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.