[英]How do I navigate between these two lists?
我正在做一个测试项目。 我的目标是能够从HomeList
导航到SampleDataList
,然后导航到基于 URL 文件类型的一些不同视图。 我不想使用 Master/Detail。
使用下面给出的代码,我收到以下错误:
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.
我努力了...
navigationDestination(for:destination:)
到HomeList
。 这是行不通的。NavigationStack
中嵌入SampleDataList
。 这也不管用。NavigationPath
。 我不确定我这样做是否正确,但它也没有用。 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)")
}
}
}
我想出了一个使用NavigationPath
的解决方案。 我向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()
}
}
}
}
}
我保持我的SampleDataList
不变。 它使用处理 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.