Still don't really understand State Restoration


I’ve read this chapter in the book - confused about a couple things:

  1. I have the book on Kindle, viewing on my Mac, and don’t see any page numbers, so I can’t give the exact location here.

In the chapter, it says:

No reason is given for this. Is this because we’re not subclassing UINavigationController, and can’t implement the UIViewControllerRestoration protocol? So we rely on the AppDelegate to do this?

  1. It’s not clear to me how child view controllers are handled. (I’m not using storyboards either).

The navigation controller the first time around in AppDelegate is initialized with the itemsViewController:

But then in the AppDelegate, for the restoration, you have:

- (UIViewController *)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder { // Create a new navigation controller UIViewController *vc = [[UINavigationController alloc] init];

So how does it know to set the root view controller to itemsViewController? It just seems weird that you call plain init here and then things happen behind the scenes that you have no control over.

Also, what about a more complex case, like a tabbed controller or some other controller that has say:

In the restoration, do you just ignore this initWithController1:Controller2:Controller3 and just go with init, and then just expect things to work with all the child controllers assigned correctly, assuming they have their own restoration set up properly?

Is there any way to get pointers to these restored child controllers and then call the proper initWithController1:Controller2:Controller3 in the parent class when restoring?

Thanks for any help.


So reading over the Apple documentation: a little more closely:

It says:

[quote]Although UIKit helps restore the individual view controllers, it does not automatically restore the relationships between those view controllers. Instead, each view controller is responsible for encoding enough state information to return itself to its previous state. For example, a navigation controller encodes information about the order of the view controllers on its navigation stack. It then uses this information later to return those view controllers to their previous positions on the stack. Other view controllers that have embedded child view controllers are similarly responsible for encoding any information they need to restore their children later.

Note: Not all view controllers need to encode their child view controllers. For example, tab bar controllers do not encode information about their child view controllers. Instead, it is assumed that your app follows the usual pattern of creating the appropriate child view controllers prior to creating the tab bar controller itself. [/quote]

So it seems like a custom controller would need to encode the child view controller itself. But it’s confusing as to whether there would be two restored versions of each child view controller then.

  1. One from decoding the child view controller within the parent view controller via decodeRestorableStateWithCoder
  2. One from the application restoration process via restorationPath/restorationClass and the child controller’s viewControllerWithRestorationIdentifierPath method.