Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace RootViewController in "Navigation-based application"

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.

like image 362
iter Avatar asked Apr 29 '11 03:04

iter


3 Answers

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];
like image 133
saadnib Avatar answered Nov 08 '22 09:11

saadnib


^ These are all ways to do it programmatically. Thats cool. But I use the interface builder and storyboards in Xcode, so this is the easy and fast way to add a new view controller:

  • Open the storyboard in question
  • Add a new view controller to your storyboard by dragging it from the objects list (right hand tool bar bottom)
  • While holding down the CONTROL key, click and drag from the middle of your navigation controller (should be blank and gray) to your new fresh white view.
  • On the popup, selection Relation Segue: Root View Controller (should be below the normal push/modal/custom options you have likely seen before)

Tada! Enjoy your new root view controller without holding your day up with programmatic creation.

like image 35
woody121 Avatar answered Nov 08 '22 10:11

woody121


Look inside the main app delegate .m file and find the method

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Inside it will be a line like this

self.window.rootViewController = self.navigationController;

You can instantiate a diffent view controller there and assign it to be the rootViewController

like image 2
ljkyser Avatar answered Nov 08 '22 11:11

ljkyser