Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Picker with Enum Source Is Not Enabled

I'm trying to understand the new SwiftUI picker style, especially with data from a source other than an array. I have built a picker with an enum. I first made a simple app with only the picker and associated enum. This works as expected.

Strangely, when I copy and paste that code into another app with other controls in the form, the picker seems to be inactive. I see it, but cannot click it.

Here's the first app (the picker works):

struct ContentView: View {

    @State private var selectedVegetable = VegetableList.asparagus

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker(selection: $selectedVegetable, label: Text("My Vegetables")) {
                        ForEach(VegetableList.allCases) { v in
                            Text(v.name).tag(v)
                            //use of tag makes no difference
                        }
                    }
                }
            }
            .navigationBarTitle("Picker with Enum")
        }
    }
}

enum VegetableList: CaseIterable, Hashable, Identifiable {
    case asparagus
    case celery
    case shallots
    case cucumbers

    var name: String {
        return "\(self)".map {
            $0.isUppercase ? " \($0)" : "\($0)" }.joined().capitalized
    }
    var id: VegetableList {self}
}

Here's the app with other controls (picker does not work).

struct Order {
    var includeMustard = false
    var includeMayo = false
    var quantity: Int = 1
    var avocadoStyle = PepperoniStyle.sliced
    var vegetableType = VegetableType.none
    var breadType = BreadType.wheat
}

struct OrderForm: View {

    @State private var order = Order()
    @State private var comment = "No Comment"
    @State private var selectedVegetable = VegetableType.asparagus
    @State private var selectedBread = BreadType.rye

    func submitOrder() {}

    var body: some View {
        Form {
            Text("Vegetable Ideas")
                .font(.title)
                .foregroundColor(.green)
            Section {
                Picker(selection: $selectedVegetable, label: Text("Vegetables")) {
                    ForEach(VegetableType.allCases) { v in
                        Text(v.name).tag(v)
                    }
                }
                Picker(selection: $selectedBread, label: Text("Bread")) {
                    ForEach(BreadType.allCases) { b in
                        Text(b.name).tag(b)
                    }
                }
            }

            Toggle(isOn: $order.includeMustard) {
                Text("Include Mustard")
            }

            Toggle(isOn: $order.includeMayo) {
                Text("Include Mayonaisse")
            }

            Stepper(value: $order.quantity, in: 1...10) {
                Text("Quantity: \(order.quantity)")
            }

            TextField("Say What?", text: $comment)

            Button(action: submitOrder) {
                Text("Order")
            }
        }
        .navigationBarTitle("Picker in Form")
        .padding()

    }
}

enum PepperoniStyle {
    case sliced
    case crushed
}

enum BreadType: CaseIterable, Hashable, Identifiable {
    case wheat, white, rye, sourdough, seedfeast
    var name: String { return "\(self)".capitalized }
    var id: BreadType {self}
}

enum VegetableType: CaseIterable, Hashable, Identifiable {
    case none
    case asparagus
    case celery
    case shallots
    case cucumbers
    var name: String {
        return "\(self)".map {
            $0.isUppercase ? " \($0)" : "\($0)" }.joined().capitalized   
    }
    var id: VegetableType {self}
}

Xcode 11 Beta 7, Catalina Beta 7 There is no behavior difference between Preview and Simulator .I must be missing something simple here. Any guidance would be appreciated.

like image 657
JohnSF Avatar asked Sep 08 '19 22:09

JohnSF


1 Answers

I wrapped the Form in a NavigationView and the pickers now operate as expected. I need to research that once the documentation is more complete but perhaps this can help someone else.

like image 156
JohnSF Avatar answered Nov 07 '22 02:11

JohnSF