gpt4 book ai didi

ios6 - 在没有 Storyboard 的 iOS 6 上保存和恢复应用程序状态

转载 作者:行者123 更新时间:2023-12-01 09:02:58 25 4
gpt4 key购买 nike

我已经按照这个 tutorial 完成了测试应用程序

我尝试在不使用 Storyboard 的情况下做同样的事情,但它不起作用。我在 AppDelegate 中启用了状态保存和恢复。我已将 restoreIdentifier 分配给我的所有 Controller 及其 View 。我想我必须在 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;
}

最佳答案

实际上,您的 View Controller 在 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 .

关于ios6 - 在没有 Storyboard 的 iOS 6 上保存和恢复应用程序状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13468182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com