使用 Storyboard,我可以让 Controller 向导航 Controller 显示/推送转场。然后,新的导航 Controller 会将 navigationController 设置为现有的导航 Controller ,新导航 Controller 的 View Controller 会将新的导航 Controller 作为其导航 Controller 。如果我尝试以编程方式推送导航 Controller ,它会失败。是否有任何编程方式来修改导航 Controller 的 View Controller ,以便它们可以包含导航 Controller ?以下将不起作用:
let aNavigationController = UIStoryboard(name: "OurStoryBoard", bundle: nil).instantiateViewControllerWithIdentifier("OurNavigationController") as OurNavigationController
navigationController!.pushViewControll(aNavigationController, animated: true)
最佳答案
NavigationControllers 不会相互插入。您可以用另一个 NavigationController 替换 NavigationController,但这不会涉及“插入”动画。
但是,您可以在同一个导航 Controller 上设置不同的 View Controller 并制作动画。这些将替换导航 Controller 中现有的 View Controller 。这是一个推送一个 View Controller 并替换导航 Controller 内任何现有 View Controller 的示例:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewControllerOne *vc1 = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerOne"];
NSMutableArray *controllers = [NSMutableArray array];
[controllers addObject:vc1];
[yourAppNavigationController setViewControllers:controllers animated:YES];
如果你想用多个 View Controller 替换当前的 View Controller :
// 1.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
FirstViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
SecondViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
ThirdViewController *vc3 = [storyboard instantiateViewControllerWithIdentifier:@"ThirdViewController"];
// 2.
NSMutableArray *controllers = [NSMutableArray array];
[controllers addObject:vc1];
[controllers addObject:vc2];
[controllers addObject:vc3];
// 3.
[yourAppNavigationController setViewControllers:controllers animated:YES];
同样,这涉及到您可能正在寻找的“插入”动画,它取代了现有的 View Controller ,因此不会有不需要的后退按钮(在第一个带有 VC 的代码示例中)。
关于ios - 如何以编程方式显示/推送导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28698743/