Changing rootviewcontroller for navigation controller dynamically


I'm trying to change RootViewController for NavigationController in didFinishLaunchingWithOptions.<br /> But I don't know how can I do that.

I have gone through this link as well:<br /><a href="http://starterstep.wordpress.com/2009/03/05/changing-a-uinavigationcontroller%E2%80%99s-root-view-controller/" rel="nofollow">http://starterstep.wordpress.com/2009/03/05/changing-a-uinavigationcontroller%E2%80%99s-root-view-controller/</a>

Here is my code in didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UIViewController *rootController=[[HomePageController alloc] initWithNibName:@"HomePageController" bundle:nil]; navigationController=[[UINavigationController alloc] initWithRootViewController:rootController]; // presentation=[[PresentationController alloc]initWithNibName:@"PresentationController" bundle:nil]; // // navigationController=[[UINavigationController alloc]initWithRootViewController:presentation]; // // presentationList=[[PresentationListController alloc]initWithNibName:@"PresentationListController" bundle:nil]; // // UINavigationController *listnavigation = [[UINavigationController alloc] initWithRootViewController:presentationList]; // // revealer=[[ZUUIRevealController alloc]initWithFrontViewController:navigationController rearViewController:listnavigation]; [self.window addSubview:navigationController.view]; [self.window makeKeyAndVisible]; return YES; }

Right now I comment and then run application to change rootviewcontroller. However this is not the practical approach.

Any help will be appreciated.


Instead of this:

[self.window addSubview:navigationController.view];

Put this:

self.window.rootViewController = navigationController;


A navigation controller does not care what type of view controller is its root view controller, as long as it is a subclass of UIViewController. So you can just use a pointer to a UIViewController like so:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIViewController *rootController = nil; if (iWantHomePageController) { rootController = [[HomePageController alloc] initWithNibName:@"HomePageController" bundle:nil]; } else if (iWantPresentationController) { rootController = [[PresentationController alloc] initWithNibName:@"PresentationController" bundle:nil]; } else if (iWantPresentationListController) { rootController = [[PresentationListController alloc] initWithNibName:@"PresentationListController" bundle:nil]; } UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootController]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = navController; [self.window makeKeyAndVisible]; return YES; }


This works really just fine for me:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; /* both *navigationController and *viewController are declared as properties in the .h file */ [self setViewController:[[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease]]; [self setNavigationController:[[[UINavigationController alloc] initWithRootViewController:self.viewController]autorelease]]; [self.window setRootViewController:[self navigationController]]; [self.window makeKeyAndVisible];


  • PFFacebookUtils login methods do nothing if Facebook app is installed
  • Is it possible to track the app with Google Analytics in IOS Simulator?
  • simple app with error “ this class is not key value coding-compliant for the key ”
  • Apple Map : get direction
  • How to Launch Specific Views on Device Type
  • UIView bottom to top animation issue
  • Display an iAd banner in landscape in swift & spritekit
  • How to check for a badge number on app launch on iPhone
  • IOS UIImageView displays with black background
  • Playing music in background ios app
  • Branch.io: javascript detect whether mobile app is installed
  • iOS: define and use C++ in Objective-C++
  • Refreshing Table Model Adapter on deleting record from it : Blackberry
  • How to add and remove rows in a UITableview dynamically in xamarin.ios
  • Universal Links while app is not running
  • iOS - Support iPad & iPhone without using nib
  • Can I put a UIScrollView inside another UIScrollView
  • Saving an array of NSURL to NSUserDefaults
  • UINavigationController alternatives
  • How to stop the horizontal scrolling programmatically?
  • Trying to use CGContextAddArc… nothing is drawn?
  • 'Terminating app due to uncaught exception'.. it couldn't find MainWindow, which no l
  • UITableView without still works!
  • Checking launchOptions in Swift 3
  • How to load image asynchronously using UIImageView+AFNetworking in ios
  • Drawing a polygon over the entire map
  • windowScriptObject method not found on Objective-C code
  • JsonConvert.DeserializeObject, Index was outside the bounds of the array
  • Printing input from TextField to a Label in Xcode with Swift
  • Drag and reorder - UICollectionview with sections
  • x-axis labels in coreplot not displayed
  • Ajax Upload File: $_FILES is empty but files exists in request header
  • Alert pop up with LWUIT
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Window Size for Mac application
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • AngularJs get employee from factory
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • How to load view controller without button in storyboard?