Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging a UINavigationController's RootViewController
    primarykey
    data
    text
    <p>I've seen the post about how to do this here <a href="http://starterstep.wordpress.com/2009/03/05/changing-a-uinavigationcontroller%E2%80%99s-root-view-controller/" rel="nofollow noreferrer">Changing a UINavigationController’s Root View Controller</a> And various other posts on SO. But... </p> <p>I've implemented a different solution which seems rather more simple, however I was wondering if there's something massively flawed about this approach since I haven't seen it used or described elsewhere and my confidence in my own code isn't very high. I also think my approach may be the cause of another issue which I'll mention at the end. </p> <p>My appDelegate has a UINavigationController variable called navController. When the game loads, I check for saved data. If it exists, I initialise the navController with the game's main view as the rootViewController. If none exists, I initialise the navController to the menu rootViewController and load that instead. If the user goes from the menu to the game, or from the game to the menu, I initialise a new rootViewController and set it to the appDelegate's navController variable, then display it. Here is the code in my appDelegate's didFinishLaunchingWithOptions method:</p> <pre><code>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]; </code></pre> <p>Here is the code when the user goes from the menu to the game:</p> <pre><code>// 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]; </code></pre> <p>And from the game to the menu:</p> <pre><code>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]; </code></pre> <p>It seems to work fine, there are no memory leaks and the transition is quick. But I have this niggling sense that it's wrong. I've left it for over a year, but recently I've been integrating the HeyZap SDK (a gaming social network where users 'check in' to a game). Logging in to HeyZap loads the HeyZap app, and then returns to the game via a URL scheme you define. If I load HeyZap from my game's menu, it returns to the game just fine. However if I load HeyZap from the game, it crashes on return (with no error output). I'm wondering if this might be because the app is confused as to which RootVC it's loading. But normally I can switch between apps just fine... I have two URL schemes - one for Facebook, and the other for HeyZap, and they look like this:</p> <p><img src="https://i.stack.imgur.com/sffhR.png" alt="info plist file"></p> <p>Any advice much appreciated! Thanks, Michael. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload