Reputation: 33
I'm making a picker. When I set the @State length
to optional, it's not working. I can't select any option.
When I set @State length
to a Double, it's working. But it associates with another picker. I want that length
can be nothing, when another picker given some option.
//this is DiskLock
struct DiskLock: Identifiable {
var name: DiskLockName
var length: Double?
var actualWeight: Double?
var id: Int
enum DiskLockName: String, CaseIterable, Identifiable {
case standard
case ledger
case diagonalBrace
case headJack
case baseJack
var id: Self { self }
}
}
// two pickers
struct EditDiskLock: View {
@ObservedObject var diskLockViewModel: DiskLockViewModel
@Environment(\.presentationMode) var presentationMode
var lengths = [0.6, 0.9, 1.2, 0.2, 0.35, 0.5, 1, 1.5, 2, 2.5, 1.61, 1.71, 1.86]
var body: some View {
VStack {
Form {
editDiskLockNameSection
editDiskLockLengthSection
editDiskLockActualWeightSection
}
addDiskLockButton
Spacer()
}
}
//selectedDiskLockName
@State var selectedDiskLockName: DiskLock.DiskLockName = .baseJack
var editDiskLockNameSection: some View {
List {
Picker("DiskLockName", selection: $selectedDiskLockName) {
ForEach(DiskLock.DiskLockName.allCases) { diskLockName in
Text(diskLockName.rawValue)
}
}.pickerStyle(.segmented)
}
}
//editDiskLockLengthSection
@State var selectedDiskLockLength: Double = 0.3 // **i want set this to optional**
var editDiskLockLengthSection: some View {
List {
Picker("DiskLockLength", selection: $selectedDiskLockLength) {
if selectedDiskLockName == .standard {
ForEach(lengths[3...9], id: \.self) { length in
Text(String(length))
}
}
if selectedDiskLockName == .ledger {
ForEach(lengths[0...2], id: \.self) { length in
Text(String(length))
}
}
if selectedDiskLockName == .diagonalBrace {
ForEach(lengths[10...12], id: \.self) { length in
Text(String(length))
}
}
}
.pickerStyle(.segmented)
}
}
//editDiskLockActualWeightSection
@State var actualWeight = ""
var editDiskLockActualWeightSection: some View {
Section(header: Text("DiskLockActualWeight")) {
TextField("DiskLockActualWeight", text: $actualWeight)
}
}
//addDiskLockButton
var addDiskLockButton: some View {
Button("add") {
if selectedDiskLockName == .baseJack || selectedDiskLockName == .headJack {
diskLockViewModel.addNewDiskLock(name: selectedDiskLockName, length: nil, actualWight: Double(actualWeight))
} else {
diskLockViewModel.addNewDiskLock(name: selectedDiskLockName, length: selectedDiskLockLength, actualWight: Double(actualWeight))
}
presentationMode.wrappedValue.dismiss()
print("\(diskLockViewModel.diskLocks)")
}.disabled(selectedDiskLockLength == 0.3)
}
}
Upvotes: 0
Views: 382
Reputation: 425
Take one nil value inside your array.
var lengths = [nil, 0.6, 0.9, 1.2, 0.2, 0.35, 0.5, 1, 1.5, 2, 2.5, 1.61, 1.71, 1.86]
and give default value to Text(String(length ?? 0.0))
Now make your variable optional @State var selectedDiskLockLength: Double?
. It will let you select options from picker.
I think the Picker is not updating with optional values, it could be because the selection value is not being set to nil properly.
Upvotes: 0