如何从导航堆栈中弹出多个视图?

How to pop multiple views off a navigation stack?

正在寻找有关在 SwiftUI 中从导航堆栈中弹出多个视图的简单方法的一些指导。 我有 4 个视图使用 NavigationLink 链接在一起。在最后一个视图中,我想跳回到初始的 ContentView,将所有其他视图从堆栈中弹出。我不想使用每个视图的 NavigationBar 上的 "Back" 按钮来实现此目的。
提前致谢。 鲍勃。 '''

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: BView()) {
                    Text("This is View A, now go to View B.")
                }
            }
        }
    }
}
struct BView: View {
    var body: some View {
        NavigationLink(destination: CView()) {
                Text("This is View B, now go to View C.")
        }
    }
}

struct CView: View {
    var body: some View {
        NavigationLink(destination: DView()) {
                Text("This is View C, now go to View D.")
        }
    }
}
struct DView: View {
    var body: some View {
        // The following line adds ContentView onto the existing navigation stack. Instead, I want to pop the previous views off the stack, leaving me back at ContentView.
        NavigationLink(destination: ContentView()) {
            Text("This is View D, now jump back to View A.")
        }
    }
}

'''

这并不是真正的 "popping" 视图脱离堆栈,但是您的 SceneDelegate 可以将 rootViewController 设置为您想要的任何视图(参见默认 SceneDelegate.swift 的第 28 行).在您的情况下,您希望它再次成为 ContentView

例如,在您的 SceneDelegate 中添加如下内容:

func toContentView() {
    let contentView = ContentView()
    window?.rootViewController = UIHostingController(rootView: contentView)
  }

然后在 DView 中,将 NavigationLink 更改为 Button 即可:

(UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.toContentView()

如果您有多个场景,则需要更多。

使 Cenk Bilgen 的回答更通用。

struct RootView {
static func change(to view: AnyView) {
    guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
      let sceneDelegate = windowScene.delegate as? SceneDelegate else {
      return
    }
    let contentView = view
    sceneDelegate.window?.rootViewController = UIHostingController(rootView: contentView)
}

}

用法:

RootView.change(to: AnyView(DashboardView()))