The picker in my code should default to no selection. But it always defaults to the first item in the array.
import SwiftUI
struct ContentView: View {
@State private var eName:String = "Dave"
@State private var eCategoryName:String = "Default"
@State private var pickerITems: [String] = ["Dodge","Ford","Chevy","Toyota"]
var body: some View {
NavigationView {
Form {
Section(header: Text("General")) {
TextField("Default", text: $eName)
Picker("Category", selection: $eCategoryName) {
//Text("Select").tag(Optional<String>(nil))
ForEach(pickerITems, id: \.self) {
Text($0).tag($0)
}
}
Text("Selected Item is: \(eCategoryName)")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
code-block
I've tried setting the variable eCategoryName to an optional and tagging it with an optional string set to nil, but it still shows the default as the first item in the picker.
If I add Text("Default").tag("") the first item isn't selectable but still shows up initially checked.
The behavior I'm looking for is nothing (blank) in the picker area, and when tapped the list of array items pops up with nothing selected allowing the user to pick the one they want.
This is iOS 16 and from what I'm seeing this behavior is new in 16.
Thanks!
1
0
980