我在添加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但畢竟這當我按下home鍵,然後再次運行應用程序我的應用程序的狀態不存在secondViewController和decodeRestorableStateWithCoder不會被調用。
我不知道我在做什麼錯?
現在我通過添加self.restorationIdentifier = @「secondViewController」來獲取輸出窗口中的所有日誌; self.restorationClass = [self class];但導航控制器總是顯示第一個視圖...請任何幫助。 –
我有同樣的問題,我有故事板中的所有控制器,你有任何解決方法..請幫助 看到這個問題我已經發布 http://stackoverflow.com/questions/25197980/issue-with-state -preservation和 - 恢復功能於IOS?noredirect = 1#comment39241814_25197980 – Sabby