带有有效CallBack HOW的SwiftUI PickerView?

问题描述 投票:0回答:1

确定-

我希望选择器视图选择一个运算符:“ =”,“ ”该操作符将作为绑定发送:

@Binding var op:String

我的选择器:

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

现在使用回叫绑定:

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

    })

因此,我可以完美地设置选择器。但是,当我返回以编辑数据时,选择器永远无法选择现有的绑定运算符,例如“

我在init中输入了:pickerSelection = operator.firstIndex(opValue)

但是由于pickerSelection是@State变量,这只会开始一个无限循环

任何人都有解决方案吗?

swiftui picker pickerview
1个回答
0
投票

这是一种有效的方法。它使用Combine使可观察到的事件可用于触发所需的事件。我也看到Combine与SwiftUI一起有用]

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

© www.soinside.com 2019 - 2024. All rights reserved.