Question

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?

Was it helpful?

Solution

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];
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top