我正在尝试使用在苹果WWDC21介绍的SwiftUI 3.0的新功能。特别是,我正在尝试使用“.searchable”修饰符在列表中实现搜索栏,并使用“NavigationLink”让用户从搜索结果导航到项目视图。
struct OrderItem : Identifiable {
let id = UUID()
var label : String
var value : Bool = false
}
struct BindingTest: View {
@State var items = [OrderItem(label: "Shirts"), OrderItem(label: "Pants"), OrderItem(label: "Socks")]
@State private var searchedText = ""
@ViewBuilder
var body: some View {
NavigationView {
List($items) { $item in
NavigationLink(destination: ItemView(item: $item)) {
HStack {
if(item.value) {
Image(systemName: "star.fill").foregroundColor(.yellow)
}
Text(item.label)
}
}
}.navigationBarTitle("Clothing")
// Search bar
.searchable(text: $searchedText, placement: .automatic) {
if searchedText != "" {
// Search result
ForEach($items.filter({$0.label.wrappedValue.contains(searchedText)})) { $item in
NavigationLink(destination: ItemView(item: $item)) {
HStack {
if(item.value) {
Image(systemName: "star.fill").foregroundColor(.yellow)
}
Text(item.label)
}
}.searchCompletion(item.label)
}
}
}
}
}
}
但是当我在搜索结果列表中并点击一个结果时,NavigationLink
不起作用,它会显示原始列表。还有其他人遇到这个问题吗?
[编辑]
我查看了这个.searchable内的NavigationLink无法工作,但不幸的是它并没有回答我的问题,因为我的NavigationLink
已经在一个NavigationView
里面。