问题
I am facing the issue in preserving the state of my application. My application is based on storyboard, this is how it goes. First I have a navigation controller, then a login screen. When user logs in, I push it to a controller, which have table view on left side, with UITabbar Items, this is mainly a custom tab bar. On clicking every button, I add that container view to the the self. The application state preserves till here, But whenever I push further, it doesn't preserve and restore the state. On the custom tab bar, I have even encode and decoded the child view controller, and add accordingly when state restores. But when I navigate further it doesn't. Further I saw a strange behavior, If I initially hit the first tab item and move further in the navigation, it preserve and restore the state further. One more thing, when I run the application on the simulator by hitting stop and play button, it shows the preserve view for few seconds and come back to the custom tab bar. Any help would be appreciated.
My Tabbar code is
// encodeRestorableStateWithCoder is called when the app is suspended to the background
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
NSLog(@"ParentViewController: encodeRestorableStateWithCoder");
[coder encodeObject:theNavigationController forKey:@"RootNav"];
[coder encodeObject:ssTodayNaVController forKey:@"TodaysCallNavEncode"];
// remember our children view controllers
[coder encodeObject:self.ssDasboardVC forKey:@"DashboardChild"];
[coder encodeObject:self.ssMessageVC forKey:@"MessageChild"];
[coder encodeObject:self.ssSearchCustomerVC forKey:SEARCH_CUSTOMER_SCENE];
[coder encodeObject:self.ssTodaysCallVC forKey:TODAYS_SCENE];
// remember the segmented control state
[coder encodeInteger:self.tabBar.mSelectedIndex forKey:@"selectedIndex"];
[super encodeRestorableStateWithCoder:coder];
}
// decodeRestorableStateWithCoder is called when the app is re-launched
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
NSLog(@"ParentViewController: decodeRestorableStateWithCoder");
theNavigationController=[coder decodeObjectForKey:@"RootNav"];
ssTodayNaVController=[coder decodeObjectForKey:@"TodaysCallNavEncode"];
self.tabBar.mSelectedIndex = [coder decodeIntegerForKey:@"selectedIndex"];
[[NSNotificationCenter defaultCenter]postNotificationName:TABBUTTON_NOTIFICATION object:[NSNumber numberWithInteger:self.tabBar.mSelectedIndex]];
[self.tabBar reloadData];
[super decodeRestorableStateWithCoder:coder];
}
And if I print the saved controllers with this method, it prints but doesn't save
- (UIViewController *)application:(UIApplication *)application
viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents
coder:(NSCoder *)coder {
// return various navigation controllers here.
// actual view controlers will each be returned in their own classes
SCLogNotice(@"Saved Controllers are %@",identifierComponents);
return nil;
}
Cheers
来源:https://stackoverflow.com/questions/25197980/issue-with-state-preservation-and-restoration-for-childviewcontroller