Reputation: 894
so right now I'm programming a game and I'm experiencing an issue I didn't really found a solution for. I'll try to describe it for you.
So, in order to be able to explain my problem better, I created a schematic drawing for you guys:
Now here's the problem: I got two View Controller, one is called MenuVc
, the other one is called GameVC
. In GameVC
I have a SKView
that shows a specific SKScene
when didMoveToView
is called, named GameScene
. GameScene
has a didBeginContact
method, which will then move to the other SKScene
the SKView
has, called GameOverScene
, with the following code (Segue 1 in the drawing):
let reveal = SKTransition.crossFadeWithDuration(0.5)
let gameOverScene = GameOverScene(size: self.size, won: false)
self.view?.presentScene(gameOverScene, transition: reveal)
From here, the user can go back to GameScene
in order to restart the Game (Segue 2 in the drawing). On GameScene
, there's still another button, which will allow you to go back to the MenuVC
via a NSNotificationCenter
event (Segue 3 in the drawing). The code for this is the following:
In GameViewController.swift:
override func awakeFromNib() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "goToMenuViewController:", name: "GoToMenuViewController", object: nil)
}
@objc func goToMenuViewController(notification: NSNotification){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MenuVC")
vc.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.presentViewController(vc, animated: true, completion: nil)
}
deinit{
NSNotificationCenter.defaultCenter().removeObserver(self)
}
In GameScene.swift, when the backButton was pressed:
NSNotificationCenter.defaultCenter().postNotificationName("GoToMenuViewController", object: self)
So if I now want to go back to GameVC
using a Button on MenuVC
(Segue 4 in the drawing), it all works fine. The SKView
in GameVC
shows the GameScene
again, but when didBeginContact()
is now called, it doesn't present GameOverScene
. The function is called, I checked that, and it executes the code in it too, but it doesn't perform the segue (1) as it should. Do you have some ideas why and what I have to change? Is it something about deiniting when Segue 3 is called?
Code for GameVC
's viewDidLoad
:
let scene = GameScene(size: view.bounds.size)
let skView = view as! SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
skView.presentScene(scene)
Code for Segue 4:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("GameVC")
vc.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.presentViewController(vc, animated: true, completion: nil)
Upvotes: 4
Views: 356
Reputation: 15377
Make sure you are dismissing each view controller before attempting to present other view controllers.
I don't think UIKit really likes it if you try to present a view controller on top of another already being presented.
Upvotes: 2