I'm writing a storyboard-based iPhone app and working on state restoration. When performing the segues normally, I want to have them animate, but when I'm restoring several levels of a navigation hierarchy, I only want the last segue to animate. Other than setting up two sets of segues—one set that uses a normal push segue, and another that uses a custom non-animating push segue—is there any way to achieve what I'm trying to do?
It's possible to directly manipulate the view controller stack, independently of the application's segues or storyboards.
You can use this technique to restore a deep stack of view controllers, and perform / animate just a single segue to the top view controller. (You will likely need to create a specific push segue for this purpose.)
For example, to restore a two view controller stack, you could do the following. In this example, it's assumed that some action on an existing view controller leads to the state restore, but you can just as easily perform it from your App Delegate.
[self performSegueWithIdentifier:@"Page2Express" sender:self];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Page2Express"])
{
// Get any state data you need to from Core Data
CoreDataType *valuePulledFromCoreData = // ...
// Set up the page 2 view controller as you normally would
Page2ViewController *page2ViewController = segue.destinationViewController;
page2ViewController.instanceVariable = valuePulledFromCoreData;
// Create a loose, page 1 view controller and set it up as required
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
Page1ViewController *page1ViewController = [storyboard instantiateViewControllerWithIdentifier:@"Page1ViewController"]; // Ensure you have this identifier set up in your storyboard
page1ViewController.instanceVariable = valuePulledFromCoreData;
// Add the page 1 view controller to the top of the navigation stack (to be later obscured in the segue by the page 2 view controller)
NSMutableArray *viewControllers = [[self navigationController].viewControllers mutableCopy];
[viewControllers addObject:page1ViewController];
[self navigationController].viewControllers = viewControllers;
}
}
If, instead, you prefer to have no animations, then it's easier still. You can restore state solely by manipulating the view controller stack (and without using any segues) from - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
and - (void)applicationWillEnterForeground:(UIApplication *)application
.
Either way will work seamlessly and in tandem with your existing storyboard(s) and segues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With