user3797886
user3797886

Reputation: 339

How to save the state of a scene using storyboard

I am new to this website and a beginning programmer making a game for iOS largely with the sprite kit on XCode and am adding a menu screen using the storyboard feature. I have found a lot of help on this website but couldn't pinpoint an exact solution to my problem when searching.

This is my setup: I have a navigation controller as the initial view controller, which goes to a view controller with one button which is the 'menu button' (on the storyboard, when I load my actual game, the button is on the top right corner and the game stuff fills in the rest of the screen). This view controller's button is linked to a second view controller, which is supposed to be the menu. The first button on the menu is supposed to 'resume game' and brings the user back to the first view controller.

Problem: However, when I click the 'menu button' (loading the second view controller) and then click "resume game" (going back to the first view controller), the game is reset. How do I save/preserve/pause/suspend the state of the game in the first view controller when I go to the menu?

Thank you for your time and help!

Upvotes: 1

Views: 215

Answers (1)

Fogmeister
Fogmeister

Reputation: 77661

OK, I think you've fallen into the trap that catches everyone at least once.

It's a guess though as there is no code or screenshot of the storyboard (these would be super useful to add to your question).

Anyway, when you go from the game screen to the menu screen you will be using a segue. The segue creates a NEW instance of the menu screen and then pushes it on to the navigation controller.

This is fine and how it is supposed to work.

I think what your problem is is that you are then using another segue to go from the menu screen to the game screen. This will do the same thing. It creates a NEW instance of the game screen.

If this is the case then you should delete the segue that goes FROM the menu screen TO the game screen.

The back button in the navigation bar should automatically handle a pop back to the game screen.

If you don't use the back button then your code (for the resume button) should be like this...

- (IBAction)resumeGame
{
    [self.navigationController popViewControllerAnimated:YES completion:nil];
}

If this isn't the case then you'll have to add more detail to the question. Code or screenshots of the storyboard.

Upvotes: 1

Related Questions