The Hit List Diary #20 – State Restoration with Custom Container View Controllers

The root of The Hit List’s storyboard looks something like this:


There’s a custom view controller at the very root. A navigation controller is embedded within that, to fill the space. From there we go on to have all the regular view controllers. Fairly simple.

Until quite recently though, we had something a bit more standard. The navigation controller was the initial view controller. I filled in restoration identifiers on the relevant scenes, and — tada! — state restoration kicked in beautifully.

Upon changing to the above setup, state restoration broke. I filled in an identifier for the main view controller, but still no dice. After searching around, I realised this snippet lives at the top of the UIViewController docs:

If you are implementing a custom container view controller, you must encode any child view controllers yourself. Each child you encode must have a unique restoration identifier.

OK. Fair enough. So whack this into your container:

That’s encoding taken care of; time to figure out decoding.

Except, what’s this? My app has now started doing what I want of restoring the child view controller without any further intervention on my part. I don’t appear to need to implement any further decoding at all! This strikes me as rather odd. Is it just a happy accident, or a deliberate design?

Update: Michael McGuire points out that Apple’s sample code takes exactly this approach

© Mike Abdullah 2007-2015