Could not cast value of type 'UIView' (0x1102beb40) to 'SKView' (0x10f0814c8)

只愿长相守 提交于 2020-01-17 05:19:14

问题


Im trying to programmatically switch from a UITableView to a SKView (Can't use the storyboard to place buttons on a TableView).

This is the code a have to switch views:

ViewController.swift :

    func game() {
    let game = GameViewController()
    present(game, animated: true, completion: nil)

}

GameViewController.swift :

import UIKit
import SpriteKit

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene(fileNamed:"GameScene") {
        // Configure the view.
        let skView = self.view as! SKView

        /* 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

        skView.presentScene(scene)
    }
}

override var shouldAutorotate : Bool {
    return true
}

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
    if UIDevice.current.userInterfaceIdiom == .phone {
        return .allButUpsideDown
    } else {
        return .all
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override var prefersStatusBarHidden : Bool {
    return true
   }
}

It is giving me the error:

Could not cast value of type 'UIView' (0x1102beb40) to 'SKView' (0x10f0814c8).

I know it is a code issue but I don't know what to fix.

CHANGING THE CLASS TO SKVIEW DOESN'T WORK!!


回答1:


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


来源:https://stackoverflow.com/questions/40199023/could-not-cast-value-of-type-uiview-0x1102beb40-to-skview-0x10f0814c8

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