0

Ok -

I want a picker view to pick one operator: "=","<",">" This operator will be sent as a binding:

@Binding var op:String

My Picker:

Picker(selection: binding, label: Text("Query Type")) {
            ForEach(0..<self.operators.count) { index in
                Text(self.operators[index]).tag(index)
            }
        }.pickerStyle(SegmentedPickerStyle())
            .padding()

Now My Binding with CallBack:

let binding = Binding<Int>(
    get: {
        return self.pickerSelection
    },
    set: {
        //pickerSelection = $0
        print("SETTTING: \($0)")
        self.op = self.operators[self.pickerSelection]
        self.queryCallback()

    })

So, I can set the pickers perfectly. BUT, when I go back to edit my data, the picker never can choose the existing bound operator, say "<"

I put in the init an:
pickerSelection = operators.firstIndex(opValue)

However this will just start an infinite loop as pickerSelection is a @State variable

Anyone have a solution?

jimijon
  • 2,046
  • 1
  • 20
  • 39

1 Answers1

0

This is a method that works. It uses Combine to make an observable one can use to trigger the needed events. Also I see how useful Combine is with SwiftUI

https://stackoverflow.com/a/57519105/810300

jimijon
  • 2,046
  • 1
  • 20
  • 39