【发布时间】:2013-06-03 02:28:40
【问题描述】:
我想在不使用情节提要的 iOS 6 应用中实现状态保存和恢复。我想要保存状态和恢复的主视图控制器是 UIViewController,它是 UINavigationController 的一部分。
我的视图控制器扩展了 UIViewControllerRestoration 协议。我想我正在实现所有必要的方法,但我没有看到模拟器对 encodeRestorableStateWithCoder 或 decodeRestorableStateWithCoder 的任何调用。
这是我的应用委托的外观: MyAppDelegate:
- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder {
NSLog(@"shouldSaveApplicationState"); // seeing this in the debug window
return YES;
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder {
NSLog(@"shouldRestoreApplicationState"); // seeing this in the debug window
return YES;
}
- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder {
NSLog(@"willEncodeRestorableStateWithCoder"); // seeing this in the debug window
}
- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder {
NSLog(@"didDecodeRestorableStateWithCoder"); // seeing this in the debug window
}
我在调试窗口中看到了所有这些调用。这是我的 MyMainViewController(在应用挂起时可见)的样子:
@interface MyMainViewController : UIViewController <UIViewControllerRestoration>
@end
@implementation MyMainViewController
- (id)init {
if (self = [super init]) {
self.restorationIdentifier = @"MyViewControllerRestoreId";
self.restorationClass = [self class];
}
return self;
}
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:frame];
// Bunch of work to create my custom views
}
- (void)awakeFromNib {
self.restorationClass = [self class];
}
// Expected a call to this method, but not seeing it in debug window
+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents
coder:(NSCoder *)coder {
NSLog(@"viewControllerWithRestorationIdentifierPath: %@", identifierComponents);
MyMainViewController *vc = [[MyMainViewController alloc] init];
vc.restorationIdentifier = [identifierComponents lastObject];
vc.restorationClass = [MyMainViewController class];
return vc;
}
// Expected a call to this method, but not seeing it in debug window
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
NSLog(@"encodeRestorableStateWithCoder");
[super encodeRestorableStateWithCoder:coder];
}
// Expected a call to this method, but not seeing it in debug window
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
NSLog(@"decodeRestorableStateWithCoder");
[super decodeRestorableStateWithCoder:coder];
}
@end
我用模拟器测试保存和恢复的方式:
1. Launch app in Simulator via Xcode
2. Invoke the Home key so App is now suspended. This where I see:
shouldSaveApplicationState
willEncodeRestorableStateWithCoder
3. End the debugging session in XCode (Command .)
4. Start a new debug session of the app in XCode. I see:
shouldRestoreApplicationState
didDecodeRestorableStateWithCoder
我不确定我还缺少什么来完成这项工作。
【问题讨论】:
-
你有这个工作吗?我被困在这里:(
-
@Jonas Gardner:您找到问题所在了吗?我也有这个问题。谢谢!
标签: ios ios6 uikit-state-preservation