我有一個帶有兩個rootViewControllers的遊戲 - 一個用於菜單,另一個用於遊戲本身。如何在2個根視圖控制器之間進行交換
當用戶在遊戲和菜單之間切換時,我想切換rootViewController。最終我的問題是,做到這一點的最佳方式是什麼?或者是否有另一種切換堆棧的方法比擁有2個rootViewController更有意義?
現在,我的appDelegate中有一個navigationController的實例。當我想切換rootViewController時,我初始化一個新的navigationController,將其設置爲rootVC,然後將其設置爲appDelegate中navController的實例。從菜單轉換到遊戲的代碼如下所示:
//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];
這是不好的做法嗎?我的應用程序在從後臺恢復時遇到問題,我認爲這可能是導致它的原因。
是否有任何具體的原因,你沒有使用Cocos2d框架來建立你的遊戲? Cocos2d在不同的場景和圖層之間切換非常容易。 – azamsharp 2012-03-05 17:10:35
當我最初構建應用程序時,我沒有看到Cocos2d,現在對它進行改造有點晚了...但是我可能會將它用於下一個應用程序。 – Smikey 2012-03-05 17:14:19