【发布时间】:2020-08-09 19:14:52
【问题描述】:
这是我的模特
struct ListItemModel: Codable, Identifiable {
let id: String
let name: String
}
这是将与 Picker 一起显示的视图。该列表将由外部来源填充,但我在此示例中对其进行了简化。
struct TypeSelectionView: View {
@State private var selected = 0
let testList = [ListItemModel(id: "11", name: "name1"),
ListItemModel(id: "12", name: "name2")]
var body: some View {
VStack {
Picker(selection: $selected, label: Text("Pick a Type")) {
ForEach(0 ..< testList.count) {
Text(testList[$0].name)
}
}.labelsHidden()
Picker(selection: $selected, label: Text("Pick a Type")) {
ForEach(testList) {type in
Text(type.name)
}
}.labelsHidden()
Text("Selected Type: \(testList[selected].name)")
Spacer()
}
}
}
struct TypeSelectionView_Previews: PreviewProvider {
static var previews: some View {
TypeSelectionView()
}
}
当 Picker 发生变化时,第一个 Picker 正确地更改了页面上 Text 视图的显示,但第二个 Picker 没有。他们是让第二个 Picker 做同样的事情的方法吗,当你更改 Picker 时,Text 视图会相应地更新 还是第一个 Picker 是您在 SwiftUI 中制作 Picker 时应该始终采用的方式?
【问题讨论】: