Reputation: 188
I'm making a small game in SpriteKit where I have a bool value stored that checks if it's the first time the user plays my game. If it is, I want the app to redirect him to another viewcontroller where he enters his character-name.
However, I'm having trouble getting my app to do so.
In my "GameViewController.swift" I have this code:
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)
}
}
However, the "setup viewcontroller" isn't loaded. I can manage to tricker the segue with a button-click, but that's not what I want. I want it to happen as soon as the view is loaded.
Any ideas?
Upvotes: 0
Views: 1631
Reputation: 83
As it was said, in viewDidLoad stage UIViewController can't be done any UI-interaction, and performSegue to, because view is not at the view hierarchy yet. You can do this at viewDidAppear(animated:Bool)
.
Upvotes: -1
Reputation: 271355
I don't know what you meant by "setup viewcontroller" isn't loaded", but usually we don't perform segues in the viewDidLoad
. At this time, the view has just been loaded into memory and has not appeared yet. Performing a segue at this time is kind of a weird thing to do.
I recommend you to perform the segue in viewDidAppear()
:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated: animated)
performSegue(withIdentifier: "GoToSetup", sender: nil)
}
Upvotes: 3
Reputation: 3068
You can programmatically call a segue with the following:
self.performSegueWithIdentifier("segueId", sender: self);
Upvotes: 1