视图:
struct ContentView: View {
let arr = [
Item(name: "Roman", addr: "Address of Roman"),
Item(name: "Alexa", addr: "Address of Alexa"),
]
@State private var selection: Item?
var body: some View {
VStack {
List {
ForEach(arr) { item in
Text(item.name!)
.id(item.name!)
.onTapGesture {
selection = item
print("selection: \(item.name ?? "")")
}
.popover(item: $selection) { item in
Text(item.addr ?? "")
}
}
}
}
}
用户单击
name
时。弹出案没有出现。如果列表中只有一个项目,则弹出声如下所示(如下图)。
当弹出案列表列表时,弹出窗口不按预期工作? 这是Swiftui中的错误?
您不能在列表中包含弹出式修饰符,因此将其移至列表本身的修饰符
List {
ForEach(arr) { item in
Text(item.name!)
.onTapGesture {
selection = item
}
}
}
.popover(item: $selection) { item in
Text(item.addr ?? "")
}
在附带说明中,不要为
Item
struct Item: Identifiable {
let id: String = UUID().uuidString
//...
}