views:

92

answers:

1

Hi,

I have the following structure in Navigation Controller

RootViewController
         |
         |--FirstViewController
         |
         |--SecondViewController

How can I jump directly from FirstViewController to SecondViewController without showing RootViewController. I would like to put a button to the FirstViewController's NavigationBar like "Go to SecondViewController."

A: 

In the appropriate button's action method, initialize the SecondViewController, then assemble an NSArray consisting of two elements: the RootViewController and the newly initialized SecondViewController (in that order, i.e. Root at index 0 and Second at index 1).

Then call the NavigationController's setViewControllers:animated: method, and pass the array of view controllers as the first argument. Remember to release the SecondViewController after calling this method, or autorelease it upon initialization to avoid a memory leak.

Just to clarify, this will cause the FirstViewController to be released by the NavigationController.

Sample:

- (void) goToSecondViewController
{
    RootViewController *root = [[self.navigationController viewControllers] objectAtIndex:0];
    SecondViewController *second = [[[SecondViewController alloc] init] autorelease];
    NSArray *controllersArray = [NSArray arrayWithObjects: root, second, nil];

    [self.navigationController setViewControllers:controllersArray animated:YES];
}

Reference: UINavigationController Class Reference

Endemic