How to swap between two root view controllers

I have a game with two rootViewControllers - one for the menu and one for the game itself.

When the user switches between the game and the menu, I want to switch rootViewController. Ultimately, my questions are: what is the best way to do this? Or is there another approach for switching stacks that makes more sense than having 2 rootViewControllers?

Be that as it may, I have an instance of navigationController in my appDelegate application. When I want to switch rootViewController, I initialize a new navigationController, set it to rootVC, and then set it to the navController instance in appDelegate. The code for moving from the menu to the game is as follows:

//Initialise the new Root Controller GameViewController *rootController = [[GameViewController alloc] init]; UINavigationController *newNavController = [[UINavigationController alloc] initWithRootViewController:rootController]; [rootController release]; newNavController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [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. MiniAppDelegate *appDelegate = (MiniAppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.navController = newNavController; [newNavController release]; 

Is this a bad practice? I have a problem with my application when it resumes from the background, and I think this may be the reason for this.

0
source share
3 answers

You might be fine if you do not present a modal view controller, but use the UIViewController , which controls the underlying view controllers.

Similar to this:

 // MainNavigationController extends UINavigationController @property (nonatomic,retain) UIViewController childViewController -(void)viewDidLoad { self.childViewController = [MenuViewController alloc] initWithNibName...]; [self pushViewController:childView...]; } -(void)launchGame { self.childViewController = [GameViewController alloc] ... ]; self.viewControllers = [NSArray array]; [self pushViewController:childView...]; } 

Thus, you constantly keep a link to your current view controller and control the display of them in one place.

You must also pass the reference to the MainNavigationController to the child view controllers so that you can use the delegate methods.

change

To talk a little about the first comment: Yes, MainNavigationController is the starting point of your application, which handles the display of the menu and the game itself.

The string self.viewControllers = [NSArray array] used to simply issue a list of the current view controllers when the game starts. This is done in order to replace the menu with a game, and not just click on it. Thus, you do not have 8 view controllers when the user goes to the menu, the game, the menu, and so on.

A similar method will be used to open the menu during the game: the button will prompt MainViewController to open the menu. You can do it the same way the launchGame method launchGame , or you can present it in a modal way to save the state of the game, or you put a smaller game menu before that or something else - many ways to handle things from there.

0
source

you can do something like this

first add GameViewController as a field to the menu controller

then use this to display it (in the menu controller class)

 if(myGameViewController == nil) { myGameViewController = [[GameViewController alloc] initWithNibName: @"GameView" bundle: nil]; } [self presentModalViewController: self.myGameViewController animated:YES]; 

and then use it to delete (in the GameViewController class)

 [self.parentViewController dismissModalViewControllerAnimated: YES]; 

What is opening a game view as a child of a menu. This requires at least the GameViewController to be a UIViewController, not a RootViewController.

0
source

Can't you just use the main menu controller as the rootView controller and use it to move any other view at the same time (or just using the new [self presentViewController:...] in iOS5)?

Even if you have several types of menus, can you always get to the main menu through the [UIApplication sharedApplication] rootView controller? or even [self presentingViewController]

0
source

All Articles