ios - 在不显示其根 VC 的情况下显示第 n 个 View Controller

标签 ios objective-c

假设我有 3 个 Controller (A、B、C)。 A和C是ViewControllers,B是NavigationController。正常的应用程序流程是 A 作为 Root View ,A 呈现(模态)B,B 推送 C。 我想要的是将 C 作为顶 View Controller 呈现,而不需要通过 A-B-C 的所有动画,但仍然具有层次结构(意味着 C 可以返回到 A),这可能吗?

我们可以将窗口 rooViewController 直接设置为 C 但它不会有层次结构

编辑: 也许我的问题不够清楚,这里的重点是,当我打开我的应用程序时,我想直接显示 C 但仍然有 A->B->C View 层次结构,所以我可以通过正常的 pop 和 dismiss

编辑 2: 我设法用 B-C 层次结构显示 C,所以我可以从 C 弹出回到 B。现在我的问题是如何从 A (ViewController) present B (NavigationController) 所以当我关闭 B 时它会**解雇*给 A

编辑 3: 我看到一些使用 NavigationController 的答案,它不是我想要的,因为通常从 A 到 B 我使用模式 presentViewController从 B 到 A 我使用 dismissViewController

编辑 4: 到目前为止我得到的是

self.window.rootViewController = vcA;
[self.window makeKeyAndVisible];
[vcA presentViewController:vcB animated:NO completion:nil];
[vcB pushViewController:vcC animated:NO];

这将给出我想要的正确层次结构,但它会给出显示 A 和 C 的快速动画(闪烁),并且还会给出警告 Unbalanced calls to begin/end appearance transitions for <vcA: 0x7fcfa0cf9c50>.

编辑5: 我最终忽略了警告并坚持我以前的答案(但仍然欢迎其他解决方案)。对于闪烁问题,我使用下面的解决方法

uiview *overlay = [new uiview]; // using vcA.frame
overlay.backgroundColor = white; // I use dominant color of vcC 
vcA addSubview:overlay;
self.window.rootViewController = vcA;
[self.window makeKeyAndVisible];
[vcA presentViewController:vcB animated:NO completion:^{
    [overlay removeFromSuperview];
}];
[vcB pushViewController:vcC animated:NO];

这将掩饰闪烁行为,因此没有人会注意到(我希望 :-p)

最佳答案

使用 UINavigationViewController然后调用

setViewControllers(_:animated:)

Use this method to update or replace the current view controller stack without pushing or popping each controller explicitly. In addition, this method lets you update the set of controllers without animating the changes, which might be appropriate at launch time when you want to return the navigation controller to a previous state.

如果启用了动画,这个方法决定哪种类型的 根据项目中的最后一项是否执行过渡 数组已经在导航堆栈中。如果 View Controller 是 当前在堆栈中,但不是最顶层的项目,此方法使用 流行过渡;如果它是最顶层的项目,则没有过渡 执行。如果 View Controller 不在堆栈上,则此方法 使用推送转换。只执行一次转换,但是当 转换完成,堆栈的全部内容是 替换为新的 View Controller 。例如,如果 Controller A, B 和 C 在堆栈上,你设置 Controller D、A 和 B,这 方法使用弹出转换,结果堆栈包含 Controller D、A 和 B。

如果对你有帮助,请告诉我:)

关于ios - 在不显示其根 VC 的情况下显示第 n 个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35914267/

相关文章:

iphone - 如何查找未翻译的字符串。 NSLocalizedString()

ios - 如何使用 Swift 在 FB 页面上发帖

ios - 如何以编程方式将触摸事件伪造到 UIButton?

ios - 从 TableViewController 滞后呈现模态 ViewController

iphone - 在 UIView 周围创建光晕

objective-c - UIActivityViewController 中共享提供者的不同数据

ios - LSApplicationWorkspace 不能在 iOS 11 上运行吗?

ios - 使用 Swift 结构和 id 时,SwiftUI 让 ScrollViewReader 滚动

objective-c - Box2D - b2body GetUserData 始终返回 null

objective-c - 发送通知到山狮通知中心