如何在2个根视图控制器之间交换 [英] How to swap between 2 root view controllers

查看:66
本文介绍了如何在2个根视图控制器之间交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个rootViewControllers的游戏-一个用于菜单,另一个用于游戏本身.

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

当用户在游戏和菜单之间切换时,我想切换rootViewController.最终,我的问题是,执行此操作的最佳方法是什么?还是有另一种切换堆栈的方法比拥有两个rootViewControllers更有意义?

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?

就目前而言,我的appDelegate中有一个NavigationController的实例.当我想切换rootViewController时,我初始化了一个新的navigationController,将其设置为rootVC,然后将其设置为appDelegate中navController的实例.从菜单过渡到游戏的代码如下:

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.

推荐答案

通过不提供模式视图控制器,而使用管理底层视图控制器的 UIViewController ,可能会很顺利.

You might be going well by not presenting a modal view controller, but to use a UIViewController that manages the underlying view controllers.

与此类似:

// 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.

还应该向两个子视图控制器传递对 MainNavigationController 的引用,以便可以使用委托方法.

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

为了使您对第一条评论有所澄清:是的, MainNavigationController 是您的应用程序的起点,它处理菜单和游戏本身的显示.

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.

self.viewControllers = [NSArray array] 这一行用于在启动游戏时清空当前视图控制器的列表.这样做是为了用游戏代替菜单,而不仅仅是推动菜单.这样,当用户转到菜单,游戏,菜单等时,您没有8个视图控制器.

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.

在玩游戏时,将使用类似的方法来打开菜单:一个按钮会要求MainViewController打开菜单.您可以采用与 launchGame 方法相同的方式进行操作,也可以然后以模态方式呈现它以保持游戏状态,或者在游戏前放置一个较小的游戏菜单这样或那样-从那里处理事情的许多方法.

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个根视图控制器之间交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆