I have a main menu that leads to a view where you play a game. From the view where you play the game you can click a button to return to the main menu, I'm using unwindSegue for this. Is there a way to now click on the same "play game" button from the main menu, replace the view, and keep the game state as it was before unwindSegue was called or will I have to store the data reload in a new view?
Asked
Active
Viewed 21 times
2 Answers
1
Yes, you can do that, but not with a segue, since they always instantiate new controllers (except for the unwind). So, you can create a property for your game view controller in the main menu, and only instantiate it if it doesn't already exist, then push it with pushViewController:animated:. Something like this,
-(IBAction)gotToGameView:(id)sender {
if (! self.game) {
self.game = [self.storyboard instantiateViewControllerWithIdentifier:@"Game"];
}
[self.navigationController pushViewController:game animated:YES];
}

rdelmar
- 103,982
- 12
- 207
- 218
0
You should store the data that is needed for recreating the view. Then, when the user enters the same screen again you can recreate a view with your saved data.

Rafał Sroka
- 39,540
- 23
- 113
- 143
-
The problem I'm having is that the game has a single player as well as turn based mode. So let's say the player is playing the single player and in the middle of the game leaves the app without giving me a chance to store the game data. Then he gets a notification telling him its his turn in an online turn-based game so when he opens it up, the single player view gets replaced and I lose the data. So I'm trying to decide if and how I should save the data, or if I should use the storyboards to pop on and off views to get back to the view with the original single player game state. – Ryan Caskey Feb 28 '14 at 20:09
-
Why not creating a two view controllers then? One will handle the single player mode and a view for it and another one would handle the turn based mode? I think the problem is that you are trying to handle these two cases in one view controller. – Rafał Sroka Feb 28 '14 at 20:13
-
I do have two view controllers, the problem was that the segue was creating a new view every time, rdelmar's answer fixed it. – Ryan Caskey Feb 28 '14 at 20:27