我希望你能在这个问题上帮助我.

I am developing this iOS application with multiple ViewControllers, as shown in the following image. enter image description here

我从VC1导航到VC2,从VC2导航到VC3,直到我到达VCN.当我到达这个视图控制器时,我需要转到VC4.我知道我可以使用到VC4的段,但问题是它会存储在导航堆栈中,这是我想避免的.

如何从VCN导航到VC4,同时清除导航堆栈(VC2、VC3...Vcn),然后从VC4返回Vc1?

我希望你能帮助我!?

推荐答案

您所需要做的就是将导航控制器的viewControllers属性设置为一个包含两个元素的数组--根视图控制器(VC1)和新的VC4.

if let navigationController {
    let vc4 = ... // initialise VC4 appropriately
    // make the array
    let newStack = [navigationController.viewControllers[0], vc4]
    // this sets viewControllers, with a "push" transition
    navigationController.setViewControllers(newStack, animated: true)
}

如果您想要"POP"转换,首先删除viewControllers中除第一个和最后一个元素之外的所有元素,然后在其余两个VC之间插入VC4.然后你可以从popViewController(animated: true)go VC4.

if let navigationController {
    let vc4 = ... 
    navigationController.viewControllers.removeSubrange(1..<navigationController.viewControllers.count - 1)
    navigationController.viewControllers.insert(vc4, at: 1)
    navigationController.popViewController(animated: true)
}

Ios相关问答推荐

Flutter应用程序无法使用IOS/Swift的蓝牙核心库发现某些外围设备

Nomb iOS 17.4.0中崩溃

如何仅在TextField中的文本发生更改时才执行代码?

在工作表上移动图像SwiftUI

当S没有身体时该如何处理

如何在SwiftUI中基于嵌套视图中的状态动态显示外部视图的内容?

SwiftData共享扩展阻止应用程序打开SQLite文件

使用 WebKit 加载 YouTube 视频时控制台中打印警告

如何在 Flutter 应用程序中测试来自 App/Play Store 的移动深层链接?

仅更改 AttributedString 的字符串(例如 UIButton 配置 attributeTitle)

如何在应用程序中显示我的小部件的快照?

使用 SceneKit 从 CapturedRoom.walls 重新创建 RoomPlan

SwiftUI:如何为 ScrollView 制作可拉伸(灵活)的粘性标题?

具有多个目标的应用程序的动态内容

如何使用情节提要重新排列 UITabBarController 项目?

由于 NSInvalidUnarchiveOperationException 导致 iOS11 WKWebview 崩溃

如何缩小 UIImage 并使其同时变得清晰/锐利而不是模糊?

如果我的分发证书过期会怎样?

如何立即移动 CALayer(无动画)

使用 xib 创建可重用的 UIView(并从情节提要中加载)