title: popToRootViewController when another tab is selected
body: <p><strong>Context:</strong></p>
  
  <p>I am using TabViewController and NavigationController at the same time. The two tabs are <code>RECENT</code> and <code>POPULAR</code> and they show a list of posts. Imagine you're inside <code>RECENT</code> tab and click a post, and you go into a <code>postsShow</code> view. So you're one deeper in a navigation stack. When you go to <code>POPULAR</code> tab and come back to <code>RECENT</code> tab, you are still seeing the post you clicked before. But I want to show a list of posts instead.</p>
  
  <p><strong>What I am trying:</strong></p>
  
  <p>I am setting <code>PostsShowViewController</code> a <code>TabBarControllerDelegate</code> and when a tab item is selected, I am trying to pop to its root view controller. Then, when the user comes back, he will see the rootViewController, which is the list of posts instead of PostsShow view.</p>
  
  <p><strong>Code:</strong></p>
  
  <p><em>viewDidAppear</em>
  <code>self.tabBarController.delegate = self;</code></p>
  
  <p><em>viewDidDisappear</em>
  <code>self.tabBarController.delegate = nil;</code></p>
  
  <p><em>header</em>
  <code>UITabBarControllerDelegate</code></p>
  
  <pre><code>- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
      [self.navigationController popToRootViewControllerAnimated:NO];
      return YES;
  }
  </code></pre>
  
  <p><strong>How it doesn't work:</strong></p>
  
  <ol>
  <li>Go to recent tab</li>
  <li>Click a post to go to PostsShow view</li>
  <li>Go to popular tab</li>
  <li>Go back to recent tab (I am expecting to see a list of posts instead of PostsShow view)</li>
  <li>Error! <code>EXC_BAD_ACCESS</code></li>
  </ol>
  
  <p><strong>EDIT:</strong>
  Following what the answers suggest doing, I get a slightly better behavior but still end up with an error.</p>
  
  <p><em>Code</em></p>
  
  <pre><code>- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
      UINavigationController *navigation = (UINavigationController*) viewController;
      [navigation popToRootViewControllerAnimated:NO];
  }
  </code></pre>
  
  <ol>
  <li>go to recent tab</li>
  <li>click post to go to PostsShowview</li>
  <li>go to popular tab </li>
  <li>go back to recent tab</li>
  <li>I see a list of posts (no error!)</li>
  <li>go back to popular tab : ERR_BAD_ACCESS!</li>
  </ol>
  
  <p><strong>EDIT:</strong>
  this  is my storyboard
  <a href="https://i.stack.imgur.com/Owg9G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Owg9G.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT2:</strong></p>
  
  <p>full stack track:</p>
  
  <pre><code>* thread #1: tid = 0x4a37c, 0x0000000197bb7bd0 libobjc.A.dylib`objc_msgSend + 16, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
      frame #0: 0x0000000197bb7bd0 libobjc.A.dylib`objc_msgSend + 16
      frame #1: 0x000000018ab52078 UIKit`-[UITabBarController _tabBarItemClicked:] + 104
      frame #2: 0x000000018a9891ec UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96
      frame #3: 0x000000018ab51fb4 UIKit`-[UITabBar _sendAction:withEvent:] + 468
      frame #4: 0x000000018a9891ec UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96
      frame #5: 0x000000018a9722c8 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 612
      frame #6: 0x000000018ab51bec UIKit`-[UITabBar(Static) _buttonUp:] + 128
      frame #7: 0x000000018a9891ec UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96
      frame #8: 0x000000018a9722c8 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 612
      frame #9: 0x000000018a988b88 UIKit`-[UIControl touchesEnded:withEvent:] + 592
      frame #10: 0x000000018a947da8 UIKit`_UIGestureRecognizerUpdate + 8536
      frame #11: 0x0000000185e8fff0 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
      frame #12: 0x0000000185e8cf7c CoreFoundation`__CFRunLoopDoObservers + 360
      frame #13: 0x0000000185e8d35c CoreFoundation`__CFRunLoopRun + 836
      frame #14: 0x0000000185db8f74 CoreFoundation`CFRunLoopRunSpecific + 396
      frame #15: 0x000000018f8136fc GraphicsServices`GSEventRunModal + 168
      frame #16: 0x000000018a9bad94 UIKit`UIApplicationMain + 1488
    * frame #17: 0x0000000100023ff4 toaster-objc`main(argc=1, argv=0x000000016fdeba50) + 124 at main.m:14
      frame #18: 0x000000019824ea08 libdyld.dylib`start + 4
  </code></pre>