I have done test app following this tutorial
I try to do the same without using Storyboards and it isn't work. I have enabled state preservation and restoration in AppDelegate. I have assigned restorationIdentifier to all my controllers and their views. I think i have to implement some additional code in AppDelegate to restore rootviewcontroller, but i cannot find the right way to do this.
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[StateTestFirstViewController alloc] initWithNibName:@"StateTestFirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[StateTestSecondViewController alloc] initWithNibName:@"StateTestSecondViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.restorationIdentifier = @"TabBarController";
self.tabBarController.viewControllers = @[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Actually, your view controllers are restored between application:willFinishLaunchingWithOptions:
and application:didFinishLaunchingWithOptions:
so if you change your code to:
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *viewController1 = [[[StateTestFirstViewController alloc] initWithNibName:@"StateTestFirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[StateTestSecondViewController alloc] initWithNibName:@"StateTestSecondViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.restorationIdentifier = @"TabBarController";
self.tabBarController.viewControllers = @[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
return YES;
}
It worked for me. Also I recommend that you watch the WWDC 2012 Session 208 — Saving and Restoring Application State on iOS.
I had quite a few issues trying to implement this as well.
First of all, have you succeeded in making this work with storyboards?
Your code looks good and you shouldn't need anything else as there are only 2 requirements:
- Set shouldRestoreApplicationState and shouldSaveApplicationState to YES in the AppDelegate
- Set restoration ID's to All the UIViewControllers (UIViews don't need it)
I would suggest you pay attention to the way you "Kill" the App.
In the simulator :
- Hit the simulator home button.
- Stop the App with Xcode.
- Play the App with Xcode.
Indeed, the system deletes your application state as soon as you kill the App from the multitask bar.
If you want it to work fine using the multitask bar, you have to set
"Application does not run in background" to YES in the Info.plist file.
Hope it helps ;)