How to replace RootViewController in "Navigation-based application"

18,335

Solution 1

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];

Solution 2

^ 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.

Solution 3

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

Share:
18,335

Related videos on Youtube

iter
Author by

iter

"Your real methodology may well be the set of feelings you are unwilling to experience"

Updated on March 01, 2020

Comments

  • iter
    iter over 4 years

    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.

  • iter
    iter about 13 years
    This is what I'm doing. I am noticing that the stock didFinishLaunching does not instantiate navigationController. I am curious who does and where, and how I can interject. Nominally, what I'm trying to avoid is doing the work twice (i.e. instantiating and immediately discarding a navigation controller). Really, I am trying to understand the behind-the-scenes magic that is XIB.
  • iter
    iter about 13 years
    This is an interesting idea. I like it better than what I'm doing now, but see my comment about magic in ljkyser's answer.
  • ljkyser
    ljkyser about 13 years
    It's actually stored in the MainWindow.xib file and is loaded lazily out of there I believe. I would think you could change the default it is using through interface builder, but I haven't done that before. We almost always do everything from code without interface builder, so we avoid that altogether.
  • iter
    iter about 13 years
    Yeah, I almost always do everything from code, too. This is an older project--a legacy of the learning curve.
  • DreamOfMirrors
    DreamOfMirrors over 12 years
    Have you ever tried this? You cannot cast a NSArray with a NSMutableArray! You must create a NSMutableArray with [self.navigationController.viewControllers mutableCopy].
  • woody121
    woody121 over 10 years
    Just because you prefer programmatic creation, doesn't mean you should downvote this answer. This works just fine.
  • Frederic Yesid Peña Sánchez
    Frederic Yesid Peña Sánchez over 10 years
    But the OP was asking about "Replacing" the RootViewController after a button click, so this is not the answer to the question.
  • tmr
    tmr about 9 years
    @woody121 this stackoverflow page was top google search result for xcode / interface builder method to change root view controller. your answer worked perfect, thank you!