Juggling multiple UINavigationControllers using the App Delegate and ensuring that the UINavigationController is correctly removed

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]; //Setting the appDelegate 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]; 

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 :)

+7
memory-management objective-c iphone delegates uinavigationcontroller
source share
1 answer

Make your life easier. Use one UINavigationController and maintain two separate View controller stacks, which are simply arrays of UIViewControllers. You can use [UINavigationController setViewControllers:animated:] to simply [UINavigationController setViewControllers:animated:] stacks and leave the nav controller in place. Use UINavigationController.viewControllers to hold the current stack before replacing it. This is about 14.3 billion times easier and cleaner than dealing with all the vagaries of several navigation controllers.

+8
source share

All Articles