Saving and Restoring Application State on iOS 6 without Storyboards

旧城冷巷雨未停 提交于 2019-12-13 13:39:10

问题


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

回答1:


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.




回答2:


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:

  1. Set shouldRestoreApplicationState and shouldSaveApplicationState to YES in the AppDelegate
  2. 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 ;)



来源:https://stackoverflow.com/questions/13468182/saving-and-restoring-application-state-on-ios-6-without-storyboards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!