在没有故事板的iOS 6上保存和恢复应用程序状态 [英] Saving and Restoring Application State on iOS 6 without Storyboards

查看:140
本文介绍了在没有故事板的iOS 6上保存和恢复应用程序状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照此教程

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

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


推荐答案

实际上,你的视图控制器是在应用程序之间恢复:willFinishLaunchingWithOptions:应用程序:didFinishLaunchingWithOptions:因此,如果您将代码更改为:

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

它对我有用。另外,我建议您观看 WWDC 2012会话208 - 在iOS上保存和恢复应用程序状态

It worked for me. Also I recommend that you watch the WWDC 2012 Session 208 — Saving and Restoring Application State on iOS.

这篇关于在没有故事板的iOS 6上保存和恢复应用程序状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆