I have an application that uses two UINavigationControllers - one for the menu system and one for the real game. In my appDelegate app, a generic UINavigationController is declared. When the application loads, it either loads the menu or the game UINavigationController. And, of course, the player can move between them.
When moving from the menu to the game, I create a new UINavigationController and present it as follows:
GameViewController *rootController = [[GameViewController alloc] init]; UINavigationController *newNavController = [[UINavigationController alloc] initWithRootViewController:rootController]; [rootController release]; newNavController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:newNavController animated:YES]; [newNavController release];
However, I noticed that when I do this, the Menu viewController never calls dealloc. Presumably because there is still a link to something supporting it. I found that when I explicitly install the App Delegate UINavigationController on the new navigation controller (before the release of the new navController), it frees up the menu. I do it as follows:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.navController = newNavController; [newNavController release];
Is this a good practice? I found that when moving from a game to a menu, however, the same trick does not seem to work. I.e
MainMenuViewController *menuViewController = [[MainMenuViewController alloc] init]; UINavigationController *newNavController = [[UINavigationController alloc] initWithRootViewController:menuViewController]; [menuViewController release]; newNavController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:newNavController animated:YES];
never call dealloc in the main ViewController of the game. And when I get back to the game again, the main menu of the ViewController menu will no longer be released.
Did I miss something when I juggled UINavigationControllers?
Thanks,
Michael
EDIT: Since then, I realized that the reason my main ViewController task was not released was due to the fact that I have some NSTimers that I did not invalidate! However, I'm still curious to find out if my approach is correct above, and that explicitly redefining navController in the App Delegate is the right way to allow different UINavigationControllers dealloc :)
memory-management objective-c iphone delegates uinavigationcontroller
Smikey
source share