presentScene导致错误的视图加载

时间:2016-09-14 13:16:59

标签: ios swift segue gameplay-kit

我有一个名为NeighborhoodViewController的viewController。它将一个场景加载到viewController中,名为NeighborhoodScene.sks。这绝对没问题。我通过NeighborhoodViewController从场景中的特定节点绑定了一个segue。这两个函数控制着这个:

func createSegue(title: String) {
    performSegueWithIdentifier(title, sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let viewController: SearchViewController = segue.destinationViewController as? SearchViewController {
        viewController.sceneName = "game_1_1"
    }
}

一切正常。正在呼叫segue。 segue旨在打造另一个叫做SearchViewController的VC,它应该调出自己的场景,SearchScene1.sks。

这是代码:

import UIKit
import SpriteKit

class SearchViewController: UIViewController {
var sceneName: String?
var chosenScene: GameScene?

override func viewDidLoad() {
    super.viewDidLoad()

    if sceneName == "game_1_1" {
        chosenScene = GameScene(fileNamed: "SearchScene1")
    } // TODO: add the rest of the presented scenes here

    if let scene = chosenScene {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsPhysics = true
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = false

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill
        scene.viewController = self
        skView.presentScene(chosenScene)
    }
}
}

如果我打印出skView的restorationidentifier,它与SearchViewController中的视图有关,这是完美的。这一切似乎都工作正常,直到skView.presentScene(selectedScene),此时它似乎随机设置了NeighborhoodViewController?如何让它调用SearchViewController和相关的sks场景 - SearchScene1?

1 个答案:

答案 0 :(得分:1)

segue用于首先使用nextviewcontroller。