How to replace RootViewController in "Navigation-based application"

iter picture iter · Apr 29, 2011 · Viewed 18.2k times · Source

I have an application that uses the "navigation-based application" template in XCode.

Now I want to change it so that the first view that loads is a regular (custom) UIView, and if the user clicks a particular button, I push the original RootViewController onto the NavigationController.

I understand that somewhere, someone is calling this with my RootViewController:

- (id)initWithRootViewController:(UIViewController *)rootViewController

I want to know how to replace the argument with my new class.

Answer

saadnib picture saadnib · Apr 29, 2011

if you want to replace the root view controller of your navigation stack you can replace the first object of its view controllers array as -

NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];

NewViewController *nvc = [[NewViewController alloc] init];
[viewControllers replaceObjectAtIndex:0 withObject:nvc];
[self.navigationController setViewControllers:viewControllers];