Bob*_*Bob 7 react-navigation-stack swiftui
寻找有关在 SwiftUI 中从导航堆栈中弹出多个视图的简单方法的一些指导。我有 4 个使用 NavigationLink 链接在一起的视图。在最后一个视图中,我想跳回初始 ContentView,从堆栈中弹出所有其他视图。我不想使用每个视图的 NavigationBar 上的“返回”按钮来实现这一点。
提前致谢。鲍勃。'''
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.")
}
}
}
Run Code Online (Sandbox Code Playgroud)
'''
Cen*_*gen 16
它并不是真正从堆栈中“弹出”视图,但是您SceneDelegate可以将 设置为rootViewController您想要的任何视图(请参阅 default 的第 28 行SceneDelegate.swift)。在您的情况下,您希望它ContentView再次出现。
例如在你SceneDelegate添加类似的东西:
func toContentView() {
let contentView = ContentView()
window?.rootViewController = UIHostingController(rootView: contentView)
}
Run Code Online (Sandbox Code Playgroud)
然后,在DView中,更改NavigationLink到Button,只是做:
(UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.toContentView()
Run Code Online (Sandbox Code Playgroud)
如果您有多个场景,则需要更多。
| 归档时间: |
|
| 查看次数: |
1514 次 |
| 最近记录: |