简体   繁体   English

如何在这两个列表之间导航?

[英]How do I navigate between these two lists?

I'm working on a test project.我正在做一个测试项目。 My goal is to be able to navigate from HomeList to SampleDataList and then to some different view that's based on the URL file type.我的目标是能够从HomeList导航到SampleDataList ,然后导航到基于 URL 文件类型的一些不同视图。 I do not want to use Master/Detail.我不想使用 Master/Detail。

With the code given below, I get the following error:使用下面给出的代码,我收到以下错误:

Note: Links search for destinations in any surrounding NavigationStack, then within the same column of a NavigationSplitView. A NavigationLink is presenting a value of type "URL" but there is no matching navigationDestination declaration visible from the location of the link. The link cannot be activated.

I have tried...我努力了...

  • Moving navigationDestination(for:destination:) to HomeList .移动navigationDestination(for:destination:)HomeList This doesn't work.这是行不通的。
  • Embedding SampleDataList in a NavigationStack .NavigationStack中嵌入SampleDataList This doesn't work either.这也不管用。
  • Using a NavigationPath .使用NavigationPath I'm not sure I did this correctly, but it didn't work either.我不确定我这样做是否正确,但它也没有用。

    struct HomeList: View {
        
        var body: some View {
            NavigationStack {
                List {
                    NavigationLink {
                        SampleDataList()
                    } label: {
                        Text("Sample Data Picker")
                    }
                }
            }
        }
    }

    extension URL: Identifiable {
        
        public var id: Int { self.hashValue }
        
    }

    struct SampleDataList: View {
        
        var urls: [URL] {
            let path = Bundle.main.resourcePath!
            let url = URL(fileURLWithPath: path).appendingPathComponent("SampleData")
            return try! FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)
        }
        
        var body: some View {
            List(urls) { url in
                NavigationLink(value: url) {
                    Text("\(url.lastPathComponent)")
                }
            }
            .listStyle(PlainListStyle())
            .navigationDestination(for: URL.self) { url in
                Text("\(url.lastPathComponent)")
            }
        }
    }

I've figured out a solution using NavigationPath .我想出了一个使用NavigationPath的解决方案。 I added a navigation path to HomeList and use an enum and navigationDestination(for:destination:) for navigating my static list...我向HomeList添加了一个导航路径,并使用枚举和navigationDestination(for:destination:)来导航我的 static 列表...

struct HomeList: View {
    
    @State var path = NavigationPath()
    
    enum HomeListOptions: String, CaseIterable {
        case sampleDataPicker = "Sample Data Picker"
        // TODO: Add more cases here
    }

    var body: some View {
        NavigationStack(path: $path) {
            List(HomeListOptions.allCases, id: \.self) { option in
                NavigationLink(option.rawValue, value: option)
            }
            .navigationDestination(for: HomeListOptions.self) { option in
                switch option {
                case .sampleDataPicker:
                    SampleDataList()
                }
            }
        }
    }
}

I kept my SampleDataList the same.我保持我的SampleDataList不变。 It uses a navigationDestination(for:destination:) that handles URLs...它使用处理 URL 的navigationDestination(for:destination:) )...

struct SampleDataList: View {
            
    var urls: [URL] {
        let path = Bundle.main.resourcePath!
        let url = URL(fileURLWithPath: path).appendingPathComponent("SampleData")
        return try! FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)
    }
    
    var body: some View {
        List(urls) { url in
            NavigationLink(value: url) {
                Text("\(url.lastPathComponent)")
            }
        }
        .listStyle(PlainListStyle())
        .navigationDestination(for: URL.self) { url in
            Text("\(url.lastPathComponent)")
        }
    }
}

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

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