title: Modifying UINavigationController corrupts navigation stack
body: <p>I've got a standard UINavigation controller and push my screens with push and pop as normal. However, i have one screen that switches between two view controllers on a button press so the screen flips over top reveal the other viewcontroller and visa versa. You can switch between them at will. </p>
  
  <p>Now, I want the back button to work as normal so I swap the top view controller to achieve this as follows:</p>
  
  <pre><code>-(IBAction)switchToSlowProductEntry:(id)sender
  {   
      NSLog(@"Switching to slow product entry");
  
      // Replace the top view with the findProductView
      FindProductView *findProdView = [ProductViewInstances shared].findProductView;
      NSMutableArray *views = [self.navigationController.viewControllers mutableCopy];
  
      [views removeLastObject];
      [views addObject:findProdView];
  
      // if sender is nil then assume we started from the viewDidLoad so no animation
      if(sender)
      {
          [UIView transitionWithView:self.navigationController.view duration:0.3 options:UIViewAnimationOptionTransitionFlipFromRight animations:^
          {
              [self.navigationController setViewControllers:views animated:NO];
          }
          completion:^(BOOL finished) {}];
      }
      else
          [self.navigationController setViewControllers:views animated:NO];
  
      NSLog(@"Views: %@", views);
      [views release];
      [ProductViewInstances shared].lastScreen = SlowProductEntryView;
  }
  
  -(IBAction)switchToQuickProductEntry:(id)sender
  {
      NSLog(@"Switching to fast product entry");
  
      // Replace the top view with the findProductView
      QuickOrderEntryView *quickProductView = [ProductViewInstances shared].quickProductView;
      NSMutableArray *views = [self.navigationController.viewControllers mutableCopy];
  
      [views removeLastObject];
      [views addObject:quickProductView];
  
      if(sender)
      {
          [UIView transitionWithView:self.navigationController.view duration:0.3 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^
           {
               [self.navigationController setViewControllers:views animated:NO];
           }
          completion:^(BOOL finished) {}];
      }
      else
          [self.navigationController setViewControllers:views animated:NO];
  
      NSLog(@"Views: %@", views);
      [views release];
      [ProductViewInstances shared].lastScreen = QuickProductEntryView;
  }
  </code></pre>
  
  <p>I have a similar piece of code for the other screen. I'm using the ProductViewInstances class to maintain the two view controllers as I do not want the classes to get unloaded as I'm maintaining stage on the screen. </p>
  
  <p>When you want to move forward from these screens, I do the push as normal to a new screen. It work and I go back after reviewing the products I added. If I press back I get back to the above screen and everything seems normal. However, when I press my custom back button (I need to do processing if back pressed) I run into a problem. </p>
  
  <p>The popViewController does nothing. Here is the code in the base class to manage the custom back button.</p>
  
  <pre><code>-(void) viewDidLoad
  {   
      self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil)
                                                                                style:UIBarButtonItemStyleBordered
                                                                               target:self
                                                                               action:@selector(myCustomBack)] autorelease];
      if(![ProductViewInstances shared].findProductView)
      {
          [ProductViewInstances shared].findProductView = [[FindProductView alloc] init];
          [ProductViewInstances shared].findProductView.customer = self.customer;
      }
      if(![ProductViewInstances shared].quickProductView)
      {
          [ProductViewInstances shared].quickProductView = [[QuickOrderEntryView alloc] init];
          [ProductViewInstances shared].quickProductView.customer = self.customer;
      }
  }
  
  
  -(void) goBack
  {
      if([[ProductViewInstances shared].quickProductView checkIfItemsPending])
      {
          // Pop up dialog
          UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Save Entries", nil)
              message:NSLocalizedString(@"Your entries will be lost", nil)
              delegate:self
              cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
              otherButtonTitles:NSLocalizedString(@"Save", nil), nil];
          [alert show];
          [alert release];
      }
      else
      {
          // Remove rows from quick item entry screen
          [[ProductViewInstances shared].quickProductView removeRowsFromtable];
          if(didPressHome)
              [self popToSpringBoard:YES];
          else
              [self.navigationController popViewControllerAnimated:YES];
      }
  }
  </code></pre>
  
  <p>So when I press back I have to check if the entries will be lost. The pop to SpringBoard pops back a couple of screens and basically calls the following:</p>
  
  <pre><code>NSArray *controllers = appDelegate.navigationController.viewControllers;
  UIViewController *springboard = [controllers objectAtIndex:2];
  [appDelegate.navigationController popToViewController:springboard animated:animated];
  </code></pre>
  
  <p>However, the popViewController animated call does nothing... Like as if it never happened.</p>
  
  <p>Donie</p>