43

I have a project with a NavigationController, that contains into IB the first ViewController to show. That's just the default pattern when creating the project.

In that first viewController, I receive some event that I send to the appDelegate, and I want it to replace that rootview with another one. Not going to a next one.

So I do :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    // Override point for customization after application launch.
    // Add the navigation controller's view to the window and display.
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void) goNext {

    [self.navigationController popViewControllerAnimated:NO];

    NextViewController* nextWindow = [[[NextViewController alloc] initWithNibName:@"NextView" bundle:nil] autorelease];
    [self.navigationController pushViewController:nextWindow animated:NO];
}

But that doesn't work, the first viewcontroller is not poped. And the second one is logically displayed with a back button.

I just want to replace that first view with the other one as the start of the navigation process.

Oliver
  • 23,072
  • 33
  • 138
  • 230
  • 1
    If the view controller at the top of the stack is the root view controller, the `popViewControllerAnimated:` method does nothing. In other words, you cannot pop the last item on the stack. You can try the `setViewControllers:animated:` method instead. – albertamg Jul 16 '11 at 12:15
  • Can't you just change your rootviewcontroller to NextViewController in xib ? – Deeps Jul 16 '11 at 12:21

3 Answers3

88

From UINavigationController reference:

This method (popViewControllerAnimated:) removes the top view controller from the stack and makes the new top of the stack the active view controller. If the view controller at the top of the stack is the root view controller, this method does nothing. In other words, you cannot pop the last item on the stack.

You can try the setViewControllers:animated: method instead.

- (void) goNext {

    NextViewController* nextWindow = [[[NextViewController alloc] initWithNibName:@"NextView" bundle:nil] autorelease];
    [self.navigationController setViewControllers:[NSArray arrayWithObject:detailViewController] animated:YES];
}
albertamg
  • 28,492
  • 6
  • 64
  • 71
  • 15
    A more modern version: `UIViewController *cont = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"MySceneId"];` `self.navigationController.viewControllers = @[cont];` – wfbarksdale Jun 03 '13 at 15:04
17

If the view controller at the top of the stack is the root view controller, popViewControllerAnimated does nothing. In other words, you cannot pop the last item on the stack.

To replace the root view controller use this method:

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
Felix
  • 35,354
  • 13
  • 96
  • 143
2

In Swift:

self.navigationController?.setViewControllers([detailViewController], animated: true)
pableiros
  • 14,932
  • 12
  • 99
  • 105