0

我在导航控制器中有以下结构

RootViewController
         |
         |--FirstViewController
         |
         |--SecondViewController

我怎样才能直接从FirstViewControllerto跳转SecondViewController而不显示 RootViewController. 我想在FirstViewControllerNavigationBar 上放置一个按钮,例如“转到 SecondViewController”。

4

1 回答 1

1

在相应按钮的操作方法中,初始化SecondViewController,然后组装一个由两个元素组成的 NSArray:theRootViewController和新初始化的SecondViewController(按此顺序,即Root在索引 0 和Second索引 1 处)。

然后调用 NavigationController 的setViewControllers:animated:方法,并将视图控制器数组作为第一个参数传递。请记住releaseSecondViewController调用此方法之后,或者autorelease在初始化时避免内存泄漏。

澄清一下,这将导致FirstViewController由 NavigationController 释放。

样本:

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

参考:UINavigationController 类参考

于 2010-07-29T15:28:42.160 回答