Search code examples
iosviewseguestack-unwinding

How can I replace the exact view that was popped off after using unwindSegue?


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?


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