我在我的應用程式中使用谷歌地圖。該應用程式會在地圖上顯示 POI 標記,并在將地圖視圖拖動一定距離時從我的 BE 加載資料。我想將該shoudlRefresh值傳遞回 ContentView 并要求用戶再次重繪 資料。
我已經使用@Binding 在我的 ContentView 和 UIViewRepresentable 之間傳遞資料,但我@Binding var shouldRefresh不能通過協調器在地圖委托中使用它。
如果我嘗試通過 coordinator 將此 @Binding var 傳遞給 coordinator init(),我會收到這兩個錯誤
at POINT A -> Property 'self.mShouldRefresh' not initialized at implicitly generated super.init call
at POINT B -> self' used in property access 'mShouldRefresh' before 'super.init' call
只有相關代碼:
struct MapsView: UIViewRepresentable {
@Binding var shouldRefresh: Bool
func makeUIView(context: Context) -> GMSMapView
func updateUIView(_ mapView: GMSMapView, context: Context)
{
func makeCoordinator() -> Coordinator {
Coordinator(owner: self, refresh: $shouldRefresh)
}
class Coordinator: NSObject, GMSMapViewDelegate {
let owner: MapsView
@Binding var mShouldRefresh: Binding<Bool>
var startPoint : CLLocationCoordinate2D?
var startRadius : Double?
init(owner: MapsView, refresh: Binding<Bool>) { // POINT A
self.owner = owner
self.mShouldRefresh = refresh // POINT B
}
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) { }
}
uj5u.com熱心網友回復:
你給了雙重系結 mShouldRefresh
只需更改為
@Binding var mShouldRefresh: Bool
在init里面改變這個
self._mShouldRefresh = refresh
另一種方式是
// Other Code
var owner: MapsView
var mShouldRefresh: Binding<Bool>
init(owner: MapsView, refresh: Binding<Bool>) {
self.owner = owner
self.mShouldRefresh = refresh
}
// Other Code
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/389104.html
