在相应按钮的操作方法中,初始化SecondViewController,然后组装一个由两个元素组成的 NSArray:theRootViewController和新初始化的SecondViewController(按此顺序,即Root在索引 0 和Second索引 1 处)。
然后调用 NavigationController 的setViewControllers:animated:方法,并将视图控制器数组作为第一个参数传递。请记住release在SecondViewController调用此方法之后,或者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 类参考