【发布时间】:2019-09-17 05:22:24
【问题描述】:
我找不到如何在 SwiftUI 的选取器视图中添加一些元素,在我的示例中,我想在单击按钮时在选取器中添加“Z”值。
struct ContentView: View {
@State var values: [String] = ["A", "B", "C"]
@State private var selectedValue = 0
var body: some View {
NavigationView {
Form {
Section {
Picker(selection: $selectedValue, label: Text("Value")) {
ForEach(0 ..< values.count) {
Text(self.values[$0])
}
}
}
Button(action: {
self.values.append("Z")
}, label: {
Text("Add")
})
}.navigationBarTitle("Select a value")
}
}
当我点击按钮时,Z 被添加到“values”数组中,但 Picker 没有刷新。
谢谢你:)
【问题讨论】: