私が持っているObservedObject
私は、フォームのユーザーからの入力に基づいてに値を渡すことTextFields
。ただし、CoreLocationを使用するオプションをユーザーに持たせたいと思います。彼らがトグルを変更するとき、私はそのうちの1つの入力値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に速度の変化が表示されることを確認したので、ロケーションパブリッシャーが機能しています。ここでバインディングソースを変更する方法がわかりません。
@Asperiによって提供された回答は、私を正しい方向に向けましたが、発行元と正しく連携しませんでした。これが私がやったことで、結局はうまくいきました:
if useGPS {
TextField("<Other_title_here>", text: self.$calculator.groundSpeed)
.onReceive(locationManager.objectWillChange, perform: { output in
self.calculator.groundSpeed = output.speed
})
} else {
TextField("Ground Speed", text: self.$calculator.groundSpeed)
}
関数locationManager.objectWillChange
内でパブリッシャーを使用するonReceive
と、変更が正しくサブスクライブされます。
私を正しい方向に向けてくれた@Asperiに感謝します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加