[英]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.