How to swap between 2 root view controllers

两盒软妹~` 提交于 2020-01-06 05:49:30

问题


I have a game with two rootViewControllers - one for the Menu and the other for the Game itself.

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

As it stands, I have an instance of a navigationController in my appDelegate. When I want to switch rootViewController, I initialise a new navigationController, set it's rootVC, then set this to the instance of the navController in the appDelegate. The code to transition from the menu to the game looks like this:

//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's 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 bad practice?? I have an issue with my app when it resumes from background and I think this might be what's causing it.


回答1:


You might be going well by not presenting a modal view controller, but to use a UIViewController that manages 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...];
}

This way you hold a reference to your current view controller all the time and manage the displaying of them in one place.

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

edit:

To clarify things a bit regarding the first comment: Yes, the MainNavigationController is the starting point of your app which handles the displaying of the menu and the game itself.

The line self.viewControllers = [NSArray array] is used to just empty out the list of current view controllers when launching a game. This is done to replace the menu with the game instead of just pushing it. This way, you don't have 8 view controllers when the user goes to the menu, to the game, to the menu and so on.

A similar method would be used to open the menu while playing the game: A button would ask the MainViewController to open the menu. You can either to it the same way the launchGame method works or you can then present it the modal way to keep the game state or you put a smaller in-game menu before that or whatsoever - many ways to handle things from there on.




回答2:


you can do something like this

first add the 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 to remove it (in the GameViewController class)

[self.parentViewController dismissModalViewControllerAnimated: YES];

What is does is open the game view as a child of the menu view This requires at least the GameViewController to be a UIViewController rather than a RootViewController.




回答3:


Can't you just use your Main Menu controller as the rootView controller and use it to push every other view modally (or just using the new [self presentViewController:...] in iOS5) ?

Even if you have multiple menu views you can always reach the main menu through the [UIApplication sharedApplication]'s rootView controller?? or even [self presentingViewController]



来源:https://stackoverflow.com/questions/9570225/how-to-swap-between-2-root-view-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!