79712233

Date: 2025-07-23 16:05:07
Score: 1.5
Natty:
Report link

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()
}
Reasons:
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): how's this
  • Low reputation (1):
Posted by: Tim