我已经像这样uinavigationcontroller
在AppDelegate.m中添加了
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:self.viewController];
nav.restorationIdentifier = @"nav1";
self.window.rootViewController = nav;
我的ViewController.m看起来像这样
- (void)viewDidLoad
{
[super viewDidLoad];
self.restorationIdentifier = @"secondViewController";
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)click:(id)sender {
secondViewController *svc = [[secondViewController alloc]initWithNibName:@"secondViewController" bundle:nil];
svc.restorationIdentifier = @"secondViewController";
svc.restorationClass = [self class];
[self.navigationController pushViewController:svc animated:YES];
}
我的secondViewController.m喜欢
+(UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
NSLog(@"viewControllerWithRestorationIdentifierPath");
UIViewController * myViewController =
[[secondViewController alloc]
initWithNibName:@"secondViewController"
bundle:nil];
return myViewController;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
label.text = @"sdfkheiowodhnskjfdgewluri3y2oejdscndjshfiledhdsfhewilufhyeows";
// Do any additional setup after loading the view from its nib.
}
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
NSLog(@"encodeRestorableStateWithCoder");
[coder encodeObject:label.text forKey:@"UnsavedText"];
[super encodeRestorableStateWithCoder:coder];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
NSLog(@"decodeRestorableStateWithCoder");
[super decodeRestorableStateWithCoder:coder];
label.text = [coder decodeObjectForKey:@"UnsavedText"];
}
在输出
我得到了 encodeRestorableStateWithCoder 但是之后当我按下主页按钮然后再次运行应用程序时,我的应用程序状态不存在 secondViewController 并且 decodeRestorableStateWithCoder 没有被调用。
我不知道我在哪里做错了?