在 Picker 上设置默认起点
Set default starting point on Picker
如何在选取器中将起始值设置为文本“请 Select 一个”。目前它默认为数组中的第一个选择。
这是状态
@State var Choioce = 0
这是选择器
var settings = ["ch1", "ch2", "ch3"]
Picker("Options", selection: $Choioce) {
Text("Please Select One")
ForEach(0 ..< settings.count) { index in
Text(self.settings[index])
.tag(index)
}
}
将选择设为可选,如下所示。用 Xcode 12 / iOS 14
测试
struct ContentView: View {
@State var Choioce: Int? // << here !!
var settings = ["ch1", "ch2", "ch3"]
var body: some View {
VStack {
Text("Selection: \(Choioce == nil ? "<none>" : settings[Choioce!])")
Picker("Options", selection: $Choioce) {
Text("Please Select One").tag(Optional<Int>.none)
ForEach(0 ..< settings.count) { index in
Text(self.settings[index])
.tag(Optional(index))
}
}
}
}
}
如何在选取器中将起始值设置为文本“请 Select 一个”。目前它默认为数组中的第一个选择。
这是状态
@State var Choioce = 0
这是选择器
var settings = ["ch1", "ch2", "ch3"]
Picker("Options", selection: $Choioce) {
Text("Please Select One")
ForEach(0 ..< settings.count) { index in
Text(self.settings[index])
.tag(index)
}
}
将选择设为可选,如下所示。用 Xcode 12 / iOS 14
测试struct ContentView: View {
@State var Choioce: Int? // << here !!
var settings = ["ch1", "ch2", "ch3"]
var body: some View {
VStack {
Text("Selection: \(Choioce == nil ? "<none>" : settings[Choioce!])")
Picker("Options", selection: $Choioce) {
Text("Please Select One").tag(Optional<Int>.none)
ForEach(0 ..< settings.count) { index in
Text(self.settings[index])
.tag(Optional(index))
}
}
}
}
}