繁体   English   中英

我无法更深入地浏览 Swiftui 中的视图

[英]I cannot navigate deeper into the views in Swiftui

我的项目中有三个视图 - ContentView、SecondView、ThirdView

在此处输入图像描述

我想从 contentView 导航到 secondView,从 secondView 导航到 thirdView。

内容视图:-

import SwiftUI

struct ContentView: View {
    @State private var path = NavigationPath()
    
    var body: some View {
        NavigationStack(path: $path) {
            Button {
                path.append("SecondView")
            } label: {
                Text("This is the first view")
                
            }
            .navigationDestination(for: String.self) { view2 in
                if view2 == "SecondView" {
                 SecondView()
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

第二视图:-

import SwiftUI

struct SecondView: View {
    @State private var path = NavigationPath()
    var body: some View {
        NavigationStack(path: $path) {
            Button {
                path.append("ThirdView")
            } label: {
                Text("This is the second view")
                
            }
            .navigationDestination(for: String.self) { view2 in
                if view2 == "ThirdView" {
                 ThirdView()
                }
            }
        }
    }
}

struct SecondView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationStack {
            SecondView()
        }
    }
}

第三视图:-


import SwiftUI

struct ThirdView: View {
    var body: some View {
        Text("This is the third view")
    }
}

struct ThirdView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationStack {
            ThirdView()
        }
    }
}

怎么了:-

每当我点击 ContentView 中的“这是第一个视图”按钮时,我都会导航到 SecondView 并自动导航回 contentView。

我想要的是:-

我想从 contentView 导航到 secondView,从 secondView 导航到 thirdView。

您不应该在层次结构中有多个NavigationStack 您应该有一个并通过Binding传递path

struct ContentView: View {
    @State private var path = NavigationPath()
    
    var body: some View {
        NavigationStack(path: $path) {
            Button {
                path.append("SecondView")
            } label: {
                Text("This is the first view")
                
            }
            .navigationDestination(for: String.self) { view in
                switch view {
                case "SecondView":
                    SecondView(path: $path)
                case "ThirdView":
                    ThirdView()
                default:
                    Text("Unknown")
                }
            }
        }
    }
}

struct SecondView: View {
    @Binding var path: NavigationPath
    var body: some View {
        Button {
            path.append("ThirdView")
        } label: {
            Text("This is the second view")
            
        }
    }
}

struct ThirdView: View {
    var body: some View {
        Text("This is the third view")
    }
}

struct SecondView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationStack {
            SecondView(path: .constant(NavigationPath()))
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM