title: Juggling multiple UINavigationControllers using the App Delegate and ensuring UINavigationController correctly deallocs
body: <p>I have a app which uses two UINavigationControllers - one for the menu system and one for the actual game. A common UINavigationController is declared in my appDelegate. When the app loads, it either loads the Menu or the Game's UINavigationController. And of course the player can then navigate between the two.</p>
  
  <p>When going from the menu to the game, I create a new UINavigationController and present it as follows:</p>
  
  <pre><code>    GameViewController *rootController = [[GameViewController alloc] init];
       UINavigationController *newNavController = [[UINavigationController alloc] initWithRootViewController:rootController];
       [rootController release]; 
       newNavController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
       [self presentModalViewController:newNavController animated:YES];
  [newNavController release];
  </code></pre>
  
  <p>However, I've noticed that when I do this, the Menu's viewController never calls dealloc. Presumably because there's still a reference to something keeping it alive. I've found that when I explicitly set the App Delegate's UINavigationController to the new navigation controller, (before releasing the new navController) it releases the Menu. I do this as follows:</p>
  
  <pre><code>MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 
   appDelegate.navController = newNavController;
   [newNavController release];
  </code></pre>
  
  <p>Is this good practice? I've found that when navigating from the game back to the menu however, the same trick doesn't seem to work. I.e. </p>
  
  <pre><code>MainMenuViewController *menuViewController = [[MainMenuViewController alloc] init]; 
   UINavigationController *newNavController = [[UINavigationController alloc] initWithRootViewController:menuViewController];
   [menuViewController release];
   newNavController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
   [self presentModalViewController:newNavController animated:YES];
  
   //Setting the appDelegate's navController to the new navController allows the menu to dealloc. 
   //This must happen AFTER the newNavController has been loaded. 
   MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 
   appDelegate.navController = newNavController;
   [newNavController release];
  </code></pre>
  
  <p>never call's dealloc on the game's main ViewController. And when I navigate back to the game again, the menu's main ViewController is no longer released either. </p>
  
  <p>Am I missing something when juggling UINavigationControllers? </p>
  
  <p>Thank you, </p>
  
  <p>Michael</p>
  
  <p>EDIT: I've since realised that the reason my game's main ViewController was not deallocing, was because I has some NSTimers that I hadn't invalidated! However, I'm still curious to know if my above approach is correct, and that explicitly redefining the navController in the App Delegate is the correct way to allow the different UINavigationControllers to dealloc :) </p>