为什么我的第二个视图在转换后没有加载?

why isnt my second view loading after transition?

我有两个视图,第一个工作正常,第二个是我从第一个视图上的按钮转换到它后尝试显示的。

但是,当我单击按钮时,发生转换并且屏幕变为空白。

下面是 UIViewController class 里面的 viewDidLoad 第二个视图。

还有当它击中

let sKView = view as! SKVIEW

它吐出的线

Could not cast value of type 'UIView' (0x10a313eb0) to 'SKView' (0x1094d5718).
(lldb)

如何让它显示视图?

class PlayViewController: UIViewController {

    var scene2: PlayScene!

override func viewDidLoad() {
    super.viewDidLoad()

    let skView = view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true

    scene2 = PlayScene(size: skView.bounds.size)

    skView.ignoresSiblingOrder = true

    /* Set the scale mode to scale to fit the window */
    scene2.scaleMode = .AspectFill

    skView.presentScene(scene2)
}

第一class

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

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

第二幕

import SpriteKit

class PlayScene: SKScene {
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        let myLabel = SKLabelNode(fontNamed:"Chalkduster")
        myLabel.text = "SCENE 2!";
        myLabel.fontSize = 65;
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

        self.addChild(myLabel)
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch in (touches as! Set<UITouch>) {
            let location = touch.locationInNode(self)

            let sprite = SKSpriteNode(imageNamed:"Spaceship")

            sprite.xScale = 0.5
            sprite.yScale = 0.5
            sprite.position = location

            let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)

            sprite.runAction(SKAction.repeatActionForever(action))

            self.addChild(sprite)
        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

我不确定您要归档什么,您可以尝试将超级 class 从 UIViewController 更改为 SpriteViewController 或者您可以创建一个新的 SKView并附加到您的视图。

您不能将 UIView 向下转换为 SKView,因为 SKView 继承自 UIView,如您在 apple documentation

中所见
Inherits From
  NSObject
    UIResponder
      UIView
        SKView

问题是您的视图控制器视图是您日常使用的 运行 工厂 UIView 而不是 SKView。假设您使用的是故事板,请单击视图控制器,然后单击视图并将 class 更改为 SKView 而不是 UIView。

希望对您有所帮助。