title: State Restoration in Tab Bar and Navigation Controller App
body: <p><strong>Goal:</strong> I'm trying to restore state on a tab controller-based app (with navigation controllers on each tab). </p>
  
  <p><strong>Problem:</strong> On relaunch, the selected tab seems to be restored as expected, but the navigation hierarchy inside said tab is not.</p>
  
  <p><strong>Development:</strong></p>
  
  <ol>
  <li>I first started with the project template "Tab based app".</li>
  <li>Next, I added restoration IDs to both child view controllers and the tab bar controller.</li>
  <li>In the app delegate, I implemented <code>application(_:shouldSaveApplicationState:)</code> and <code>application(_:shouldRestoreApplicationState:)</code>.</li>
  </ol>
  
  <p>I run then app, switch to the second (right) tab, hit home, terminate. o  relaunch, the right tab is displayed (as expected). <strong>So far so good.</strong></p>
  
  <ol start="4">
  <li>Next, I go to the storyboard and embed both child view controllers in respective navigation controllers, and assign restoration IDs to those too.</li>
  </ol>
  
  <p>I run the app, and restoration still works. <strong>Still good.</strong></p>
  
  <ol start="5">
  <li><p>Next, I add a "detail" view controller; its class is a custom subclass of <code>UIViewController</code> to the storyboard, with properties to configure the contents of a debug label and its view's background color.</p></li>
  <li><p>I placed a "Show Detail..." button on each of the tabs' top view controllers, and create a segue from each into the (shared) detail view controller. So now my storyboard looks like a hexagon (also, both segues have identifiers set in Interface Builder). So, both left and right top view controllers share the same type of "detail" view controller. On show, it is configured so as to distinguish from where it has been pushed (see next point).</p></li>
  <li><p>On each of the top view controllers' <code>prepareForSegue(_:sender:)</code> method, I configure the pushed detail view controller differently: Different text and background color ("left" and blue, and "right" and red, respectively). </p></li>
  <li><p>I added code to the detail view controller to save and restore the state of the text and background color properties: <code>encodeRestorableStateWithCoder(_:)</code> and <code>decodeRestorableStateWithCoder(_:)</code>. Also, I implemented <code>viewDidLoad()</code> so as to reflect those properties' values in the view. Whenever it is instantiated and pushed into the navigation through a segue, the properties are first set and then used to configre the view in <code>viewDidLoad()</code>. Whenever it is instantiated during restoration, the properties are set in <code>decodeRestorableStateWithCoder(_:)</code> and similarly used in <code>viewDidLoad()</code>.</p></li>
  </ol>
  
  <p>...but when I run this code, <strong>the last selected tab is restored but only up to the top view controller</strong> -left or right-, <strong>not the detail</strong>. Interestingly, the background color last set to the detail view controller flashes for an instant.</p>
  
  <p>I placed break points in <code>encodeRestorableStateWithCoder(_:)</code> and <code>decodeRestorableStateWithCoder(_:)</code>, but only the first of them is executed (<em>encode</em>).</p>
  
  <ol start="9">
  <li>Wondering what might be missing, I went ahead and implemented the app delegate's <code>application(_:viewControllerWithRestorationIdentifierPath:coder:)</code>(returning always nil, but logging the path components passed).</li>
  </ol>
  
  <p>The documentation is not very clear on whether this method is needed or not, and in any case all view controllers except the detail seem to be restored perfectly even without it. I added code to instantiate each view controller based on the last path component (i.e., that controller's restoration ID) and returning it.</p>
  
  <p>Now, the <code>decodeRestorableStateWithCoder(_:)</code> <em>is</em> called, but the navigation still goes back to the tab's top view controller after a split second.</p>
  
  <p>So, what is going on? What am I missing to implement State Preservation and Restoration in a Tab Bar + Navigation Controller app?</p>