how's this?
I had to get rid of the sub picker each time the category changed... redundant code... but this worked...
struct ContentView2: View {
enum Category: String, CaseIterable, Identifiable {
case typeA , typeB , typeC
var id: Self { self }
var availableOptions: [ Option ] {
switch self {
case .typeA : return [ .a1 , .a2 ]
case .typeB : return [ .b1 , .b2 ]
case .typeC : return [ .c1 , .c2 ]
}
}
}
enum Option: String, CaseIterable, Identifiable {
case a1 , a2 , b1 , b2 , c1 , c2
var id: Self { self }
}
@State private var selectedCategory: Category = .typeA
@State private var selectedOption: Option = .a1
var body: some View {
Form {
Section ( header: Text ( "Selection" ) ) {
HStack {
Picker ( "" , selection: $selectedCategory ) {
ForEach ( Category.allCases ) { category in
Text ( category.rawValue.capitalized ) .tag ( category )
}
}
Spacer()
switch selectedCategory {
case .typeA:
Picker ( "" , selection: $selectedOption ) {
ForEach ( self.selectedCategory.availableOptions ) { option in
Text ( option.rawValue.uppercased() ) .tag ( option )
}
}
case .typeB:
Picker ( "" , selection: $selectedOption ) {
ForEach ( self.selectedCategory.availableOptions ) { option in
Text ( option.rawValue.uppercased() ) .tag ( option )
}
}
case .typeC:
Picker ( "" , selection: $selectedOption ) {
ForEach ( self.selectedCategory.availableOptions ) { option in
Text ( option.rawValue.uppercased() ) .tag ( option )
}
}
}
}
}
.labelsHidden()
.pickerStyle ( .menu )
}
}
}
#Preview {
ContentView2()
}