假设我们有两个非常基本的类:
- 一个名为
Item
的NSManagedObject
类 - 以及一个名为
Navigation
的ObservableObject
类来处理NavigationPath
final class Navigation: ObservableObject {
@Published var path = NavigationPath()
}
我希望在NavigationPath
包含特定Item
元素时禁用NavigationLink
,如下例所示:
struct ContentView: View {
@StateObject private var navigation = Navigation()
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], animation: .default)
private var items: FetchedResults<Item>
var body: some View {
NavigationStack(path: $navigation.path) {
List(items) { item in
NavigationLink(value: item) {
Text("Item at \(item.timestamp!.formatted())")
}
.disabled(navigation.path.contains(/* some Item */))
}
}
.environmentObject(navigation)
}
}
然而,令我惊讶的是,一个NavigationPath
没有contains()
的功能。有什么想法吗?
1条答案
按热度按时间elcex8rz1#
你换吧
至