更改UINavigationController的RootViewController

时间:2012-02-27 14:03:51

标签: iphone objective-c xcode uinavigationcontroller uiapplicationdelegate

我在这里看到了有关如何执行此操作的帖子Changing a UINavigationController’s Root View Controller以及SO上的各种其他帖子。但是......

我已经实现了一个看起来更简单的不同解决方案,但是我想知道这个方法是否存在严重缺陷,因为我没有看到它在其他地方使用或描述过,而且我对自己的代码的信心不是很高高。我也认为我的方法可能是另一个问题的原因,我最后会提到这个问题。

我的appDelegate有一个名为navController的UINavigationController变量。当游戏加载时,我检查保存的数据。如果它存在,我将navController与游戏的主视图初始化为rootViewController。如果不存在,我将navController初始化为菜单rootViewController并加载它。如果用户从菜单进入游戏,或从游戏进入菜单,我会初始化一个新的rootViewController并将其设置为appDelegate的navController变量,然后显示它。这是appDelegate的didFinishLaunchingWithOptions方法中的代码:

NSFileManager *fileManager = [[NSFileManager alloc] init];
    if ([fileManager fileExistsAtPath:[self dataFilePath]]) { 
        self.saveDataExists = TRUE;
        GameViewController *gameViewController = [[GameViewController alloc] init]; 
        UINavigationController *gameNavController = [[UINavigationController alloc] initWithRootViewController:gameViewController];
        [gameViewController release];
        self.navController = gameNavController;
        [gameNavController release]; 
    }
    else {
        self.saveDataExists = FALSE;
        MainMenuViewController *mainMenuViewController = [[MainMenuViewController alloc] init];
        UINavigationController *menuNavController = [[UINavigationController alloc] initWithRootViewController:mainMenuViewController];
        [mainMenuViewController release];
        self.navController = menuNavController;
        [menuNavController release];
    }

[fileManager release];
    [window addSubview:navController.view];
    [window makeKeyAndVisible];

以下是用户从菜单进入游戏时的代码:

// 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. 
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 
appDelegate.navController = newNavController;
[newNavController release];

从游戏到菜单:

MainMenuViewController *menuViewController = [[MainMenuViewController alloc] init]; 
UINavigationController *newNavController = [[UINavigationController alloc] initWithRootViewController:menuViewController];
[menuViewController release];
newNavController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[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.  
self.appDelegate.navController = newNavController;
[newNavController release];

似乎工作正常,没有内存泄漏,转换很快。但我有这种琐碎的感觉,这是错误的。我已经离开了一年多了,但最近我一直在整合HeyZap SDK(一个游戏社交网络,用户可以在这里登录游戏)。登录HeyZap会加载HeyZap应用程序,然后通过您定义的URL方案返回游戏。如果我从我的游戏菜单中加载HeyZap,它就会很好地返回游戏。但是如果我从游戏中加载HeyZap,它会在返回时崩溃(没有错误输出)。我想知道这可能是因为应用程序对它加载的RootVC感到困惑。但通常我可以在应用程序之间切换就好......我有两个URL方案 - 一个用于Facebook,另一个用于HeyZap,它们看起来像这样:

info plist file

任何建议都非常感谢!谢谢,迈克尔。

0 个答案:

没有答案