在没有故事板的iOS 6上保存和恢复应用程序状态

时间:2012-11-20 07:09:46

标签: ios6 storyboard xib state-saving

我已按照此tutorial

完成了测试应用

我尝试在不使用Storyboard的情况下做同样的事情,但这不起作用。我在AppDelegate中启用了状态保存和恢复。我已经为我的所有控制器及其视图分配了restorationIdentifier。我想我必须在AppDelegate中实现一些额外的代码来恢复rootviewcontroller,但我找不到正确的方法来做到这一点。

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

2 个答案:

答案 0 :(得分:5)

实际上,您的视图控制器会在application:willFinishLaunchingWithOptions:application:didFinishLaunchingWithOptions:之间恢复,因此如果您将代码更改为:

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

它对我有用。另外,我建议您观看WWDC 2012 Session 208 — Saving and Restoring Application State on iOS

答案 1 :(得分:4)

我也试图实现这个问题。

首先,您是否已成功使用故事板进行工作?

您的代码看起来不错,您不需要任何其他内容,因为只有2个要求:

  1. 在AppDelegate中将shouldRestoreApplicationState和shouldSaveApplicationState设置为YES
  2. 将恢复ID设置为所有UIViewControllers(UIViews不需要它)
  3. 我建议你注意你“杀死”应用的方式。

    在模拟器中:

    • 点击模拟器主页按钮。
    • 使用Xcode停止应用程序。
    • 使用Xcode播放应用程序。

    实际上,只要您从多任务栏中删除应用程序,系统就会删除您的应用程序状态。

    如果您希望使用多任务栏正常工作,则必须设置 “应用程序不在后台运行”到Info.plist文件中的“是”。

    希望它有所帮助;)