Could not cast value of type 'UIView' (0x112484eb0) to 'SKView' (0x111646718)
I finally fixed it! Instead of putting the functions in the GameViewController and calling them from game scene, I had to put the functions in gameScene and replace view with self.view!.
Swift Could not cast value of type 'UIView' to 'SKView'
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. The first way to fix this is, as said by Nathan Hitchings in the comments, to override loadView
and set 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
. 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. 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
}
Could not cast value of type 'UIView' to 'SKView' (removed Storyboard)
This is where your mistake is:
let skView = view as! SKView
You are assuming that self.view
is an instance of SKView
, but it isn't. It's a UIView
, just like how in the storyboard, the root view by default has the class UIView
.
You should instead do:
let skView = SKView()
view = skView
To set the root view to an instance of SKView
.
Could not cast value of type 'UIView' (0x1102beb40) to 'SKView' (0x10f0814c8)
i had the same problem before. Go to your storyboard and in your identity inspector, set custom class to SCNView
//set custom class to SCNView
Could not cast value of type 'UIView' to 'SCNView'
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
How to bring an SKSprite in front of a storyboard's UI?
You should add UIView to Storyboard, then define UIView as SKView. You can check how to do it: Could not cast value of type 'UIView' (0x112484eb0) to 'SKView' (0x111646718) Then you will be able present SKScene inside this SKView.
let skView = self.view as! SKView
skView.presentScene(GameScene(size: skView.bounds.size))
SKView to UIView
SKView
is a subclass of UIView
. Everywhere you can use a UIView
, you can also use an SKView
. You can
go from an
SKView
to anUIView
simply by casting it. For example:
let view = spriteKitView as UIView
Thread 1 signal SIGABRT crash when casting UIView to SKView
You are getting a crash because your UIView's (i.e. your self.view) actual type is NOT an SKView. So you can't downcast to an SKView because it is not an SKView. So wherever you create your ViewController's view you must be assigning it a UIView. You said you are not using storyboards so I assume you are creating the ViewController programmatically. If this is the case, you need to manually assign your ViewController's view an SKView. This is typically done in the loadView method of the ViewController. You can see an example of this here
Related Topics
How to Take a Snapshot of a Uiview That Isn't Rendered
Why Is an Nsdate in a Core Data Managed Object Converted to Nstimeinterval
Understanding Model-View-Controller
What Does $0 Represent in Closures in Swift
How to Create Multi Line Uisegmentedcontrol
Uiapplicationlaunchoptionsremotenotificationkey Not Getting Userinfo
Swift Spritekit Facebook Share Button
Find Favorite Contacts from the iOS Address Book API
How to Change the Height of Uitextfield in Uialertcontroller in Swift
Multiple Localized .Strings Files in iOS App Bundle
App Crashes in Background While Changing Permission - Swift
Add Uiimage on Top of Another Uiimage
A Lighter Way of Discovering Text Writing Direction
Undefined Symbols for Architecture Armv7 for Cocoapods Libraries