在NavigationController中推送多个UIViewControllers

4
我有6个已经子类化的UIViewControllers并通过标识符连接了推送segue。
它们按照A>B>C>D>E>F的顺序进行跳转。我无法找到一种方法来实现在控制器A中添加一个按钮,该按钮将自动堆叠所有控制器直到控制器F并显示F控制器。 堆叠应该在UINavigationController实例中完成,而不是通过(void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated。因为如果我使用setViewControllers方法,那么我会失去segue标识符。谢谢!
1个回答

8
您可以使用pushViewController:animated:实现此操作,如下所示:
// This method belongs to view controller A class
-(void)pushToF {
    // I am assuming that A is already in the navigation controller
    UINavigationController *nav = self.navigationController;
    UIViewController *b =[self.storyboard instantiateViewControllerWithIdentifier:@"B"];
    [nav pushViewController:b animated:NO];
    UIViewController *c =[self.storyboard instantiateViewControllerWithIdentifier:@"C"];
    [nav pushViewController:c animated:NO];
    ... // And so on, until F
    UIViewController *f =[self.storyboard instantiateViewControllerWithIdentifier:@"F"];
    // You can push the last one with animation, so that end users would see it
    [nav pushViewController:f animated:YES];
}

非常完美,非常感谢!还有一个问题。如果我在控制器A上实现模态视图,并且在该模态视图中有一个pushToF方法,那么代码会是什么样子的?我知道我必须先以编程方式解除模态视图,然后执行您编写的内容,但我不知道如何构建。感谢您的帮助。 - Snsa Kscc
@AndrejTrilavov 如果 "A" 是模态的,那么你需要以不同于 self.navigationController 的方式获取导航控制器的引用。此外,你也可以在按钮上调用 dismiss,并从 viewWillDisappear:animated: 中调用 pushToF - Sergey Kalinichenko
很不幸,我在VC上还有其他按钮,所以无法使用viewWillDisappear:animated:。我知道我必须在self上执行dismissViewControllerAnimated:NO并实现你提供给我的代码,但不知道如何编写它。谢谢! - Snsa Kscc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接