Reputation: 11
so I have a basic game going in Xcode. I started with Xcode's basic game template and built my game mostly inside the 'GameScene.swift'. When I finished with my game I wanted to add a start screen with a play button so I made a new UIViewController and added a button. I control click and dragged the button to the GameViewController and created a Modal Segue. I then wanted the game to go back when the player died. I tried various ways to dismiss the view but none worked. I am new to swift and really need help. Let me know what code/info is needed to find a solution. I have tried everything I have found on the internet. I thought I found a way around it by adding a button and Segue to the GameViewController to the menu but after multiple pushed it clogged the system and slowed to a crawl because none were dismissed. I can provide any code needed.
Inside of GameViewController.swift in the GameViewController class I tried making a function that was called when the game ended. I tried to both pop and dismiss the view controller. The function was called and a line printed to console but the view remained. The only thing printed to console is 'nil'
class GameViewController: UIViewController {
func end(){
print(navigationController?.viewControllers as Any)
self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: nil)
}
Here is the screenshot of the storyboard.
thanks in advance.
Upvotes: 0
Views: 10230
Reputation: 116
I have a modal segue in a storyboard from one view controller to the other. Then I have a button that's connected to an IBAction that just runs
dismiss(animated: true, completion: nil)
Check and redo the connections from the storyboard to the code. Xcode sometimes just looses a connection when making a lot of changes to the code.
Upvotes: 0
Reputation: 1915
If you pushed the viewController you use self.navigationController?.popViewController(animated: true)
If you presented it modally you use
self.dismiss(self, animated: true)
When its presented from a modal segue you use
self.presentingViewController?.dismiss(animated: true, completion: nil)
Upvotes: 15