Swift执行segue而不点击按钮

时间:2017-07-07 20:13:48

标签: ios swift segue

我在SpriteKit中制作了一个小游戏,我存储了一个bool值,用于检查用户是否第一次玩游戏。如果是,我希望应用程序将他重定向到另一个视图控制器,在那里他输入他的角色名称。

但是,我无法让我的应用程序这样做。

在我的" GameViewController.swift"我有这段代码:

override func viewDidLoad() {
    super.viewDidLoad()

    checkGame()

    if let view = self.view as! SKView? {

        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "GameScene") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            // Present the scene
            view.presentScene(scene)
        }

        view.ignoresSiblingOrder = true

        view.showsFPS = true
        view.showsNodeCount = true
    }
}

func checkGame() {
    //Check game
    let firstPlay:Bool = appData.bool(forKey: "\(GameData.firstPlay)")
    print(firstPlay)

    if firstPlay == false {
        print("Heading for setup")
        self.performSegue(withIdentifier: "GoToSetup", sender: nil)
    }
}

然而,"设置viewcontroller"没有加载。我可以通过点击按钮来设法刺激segue,但这不是我想要的。我希望它一加载视图就会发生。

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

我不知道你的意思是什么" setup viewcontroller"没有加载",但通常我们不会在viewDidLoad中执行细分。此时,视图刚被加载到内存中并且尚未出现。在这个时候执行一个segue是一件很奇怪的事情。

我建议你在viewDidAppear()中执行segue:

 override func viewDidAppear(animated: Bool)  {
    super.viewDidAppear(animated: animated)
    performSegue(withIdentifier: "GoToSetup", sender: nil)
}

答案 1 :(得分:1)

您可以使用以下命令以编程方式调用segue:

batch/bat

答案 2 :(得分:-1)

正如所说,在viewDidLoad阶段,UIViewController无法进行任何UI交互,并且执行Sese,因为视图尚未处于视图层次结构中。您可以在viewDidAppear(animated:Bool)

执行此操作