我有一个ObservedObject
,我根据来自表单TextFields
的用户输入将值传递到其中。但是,我希望用户可以选择使用CoreLocation。当他们更改切换时,我希望TextFields
之一的输入值切换到我的CoreLocation
发布者。以下是代码段:
@EnvironmentObject var locationmanager: Locationmanager
@ObservedObject var calculator: CalculatorObject
@State var useGPS: Bool = false
if self.useGPS {
//I'm not sure what to put here
//I’ve tried several options to set the binding element of the
// CalculatorObject to the speed object of the
// locationmanager but they don’t change the values within
// the calculations.
}
var body: Some View {
VStack {
Toggle(isOn: $useGPS) {
Text("Use GPS for Ground Speed")
}
if useGPS {
Text(locationmanager.locationInfo.speed)
} else {
TextField("Ground Speed",text: self.$calculator.groundSpeed)
}
}
}
我尝试了许多不同的选项,但是我似乎无法从位置管理器获取数据以将其数据传递到CalculatorObject.
,我已经验证了在更改切换开关时,UI 显示更改的速度,因此我确定位置发布程序正在运行。我不清楚如何更改绑定源。