title: How do I ensure that the right ViewController is loaded when accepting a file?
body: <p>EDIT: One question left at bottom - how to ensure I don't call methods before any VC presented by a navigation controller is properly finished being popped.</p>
  
  <p>EDIT: <a href="http://useyourloaf.com/blog/2010/08/30/popping-tabbed-navigation-controllers.html" rel="nofollow">An useful tutorial in doing pretty much what I want, but not quite.</a></p>
  
  <p>So I have this setup</p>
  
  <p>Initial VC -> Tab controller -> (three VC's, one of them...) NavController containing file list -> File editor</p>
  
  <p>At any time, I might get a application:handleOpenUrl message sent to my app delegate that tells me that the user wants to edit a file, for example an attachment from an email. Depending on the state of my app, all or some of these VC's might be loaded. In order to react sensibly to the URL, I need for the NavController to be loaded at the very least.</p>
  
  <p>I suppose the real objective is to get the app in a state where the NavController is loaded, and the currently active controller, so I can send it a message from the AppDelegate to load a given file once it's processed.</p>
  
  <p>So: What is best practice here?</p>
  
  <p>EDIT</p>
  
  <p>So I played around a bit, and got this far:</p>
  
  <p>I know that the root VC is a UITabBarController, and is always loaded.</p>
  
  <p>I know that my NavigationController is at index 2</p>
  
  <p>I know that it either has loaded the NC, or will do so on demand if I switch to the tab</p>
  
  <p>So the algorithm is then: 
  Grab the root tabbar VC. Tell it to switch to tab 2 if not there already. Grab the NavigationController at tab 2. Pop it to root, and tell it what to do.</p>
  
  <p>Now my only issue is that in this code, the action that I invoke on the root VC of the Navigation controller is performed BEFORE any VC currently presented by the NC has a change to do viewWillDisappear.</p>
  
  <pre><code>UITabBarController *root = (UITabBarController*) self.window.rootViewController;
  if (root.selectedIndex != 2) {
      root.selectedIndex = 2;
  };
  UINavigationController *nc = root.viewControllers[2];
  [nc popToRootViewControllerAnimated:YES];
  NSObject&lt;SGPTFileManagerViewProtocol&gt; *fileManager = (NSObject&lt;SGPTFileManagerViewProtocol&gt; *) nc.visibleViewController;
  
  // Whoops! This method is invoked before the navigation controller is done popping a presented view controller!
  [fileManager METHOD-FOR-HANDLING-THE-URL-HERE];
  </code></pre>