ios - 如何以编程方式显示/推送导航 Controller

标签 ios objective-c swift

使用 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/

相关文章:

image - 可以从非 Apple 平台生成用于 iOS 显示的最佳 PNG 内部格式是什么?

ios - Google 地点自动完成功能无法在 iPhone 上运行

objective-c - 让 __PRETTY_FUNCTION__ 只打印选择器的第一部分?

ios - uitableview 中的 FBProfilePictureView 每次都会自行重新创建

ios - iAD 广告未显示在应用商店应用程序中

ios - 如何在发布版本中使用 APS 开发证书?

iphone - 尝试显示图像数组,代码将 iPhone 返回到主屏幕

swift - 在 Swift 中实现自动大写

swift - 如何使用 Swift 中的 where 检查/将类转换为泛型类型

ios - 理解 SwiftUI 的显式对齐