ios - 从模态呈现的视图控制器导航到根视图控制器
问题描述
我有以下情况。
根视图控制器 - A
推送视图控制器 - B
提出的视图控制器 - C
A -> 推 B。
B -> 呈现 C。
我怎样才能从C回到A。
解决方案
如何?
为此,您必须将呈现控制器UINavigationController
作为变量传递给您正在使用的视图控制器present
。让我告诉你如何,以及结果。
从vcA
到推动vcB
是相当直截了当的。需要注意的一件事是,当您从vcA
to推送时vcB
,vcA
将位于导航堆栈中。考虑到这一点,让我移动一个。
首先vcC
通过添加一个变量来保存所UINavigationCongroller
呈现的视图控制器的vcC
,这将是vcB
。执行以下操作(阅读评论)
class ViewControllerC: UIViewController {
// Variable that holds reference to presenting ViewController's Navigtion controller
var presentingNavigationController: UINavigationController!
//Some action that triggers the "Go-back-to-A"
@objc func pressed() {
// When the completion block is executed in dismiss,
// This function will loop through all ViewControllers in the presenting Navigation stack to see if vcA exists
// Since vcA was earlier pushed to the navigation stack it should exist
// So we can use the same navigation controller to pop to vcA
// Set the animated property to false to make the transition instant
dismiss(animated: false) {
self.presentingNavigationController.viewControllers.forEach({
if let vc = $0 as? ViewController {
self.presentingNavigationController.popToViewController(vc, animated: true)
}
})
}
}
您可以在vcB
其中添加以下present(_:_:_:)
功能
// function call
@objc func pressed() {
let vc = ViewControllerC()
// Setting the navigation controller for reference in the presented controller
vc.presentingNavigationController = self.navigationController
present(vc, animated: true, completion: nil)
}
结果
推荐阅读
- reactjs - 对等依赖项即使在手动安装后也没有安装
- javascript - 我想问,如何将一个值从 ajax 扔到 javascript?
- android - Espresso Android 测试 - 检查 IntroductoryOverlay 是否处于活动状态
- php - Composer 安装 Symfont PHP 版本错误
- java - 春季启动请求验证不起作用
- laravel - 使用路由助手时如何将数据添加到参数,所需的数据存储在变量中
- macos - 如何在 MAC 上从 1.2 升级 glsl 版本
- java - 从java中嵌入的JSON字符串中提取值
- opengl - opengl 和 GLFW3 简单的三角形绘制,看不到三角形
- mysql - mariaDB 10.1.44-MariaDB-0+deb9u1:如何更改空白根密码?