好-
我想要一个选择器视图来选择一个运算符:“ =”,“ <”,“>” 该操作符将作为绑定发送:
@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变量,这只会启动无限循环
有人解决吗?
答案 0 :(得分:0)
这是一种有效的方法。它使用Combine使可观察到的事件可用于触发所需的事件。我也看到Combine与SwiftUI一起有用