我正在做一个测试项目。我的目标是能够导航从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.
I have try…
- 将
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:)
导航我的静态列表…
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
相同。它使用navigationDestination(for:destination:)
来处理url…
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)")
}
}
}