我在我的应用程序中使用谷歌地图。该应用程序在地图上显示POI标记,并在地图视图按一定距离拖动时从my BE加载数据。我希望将shoudlRefresh
值传回给ContentView,并要求用户再次刷新数据。
我使用@Binding在我的ContentView和UIViewRepresentable之间传递数据,但是我不能通过协调员在映射中使用这个@Binding var shouldRefresh
。
如果我试图通过协调器init()
将这个@Binding传递给协调器,我将得到以下两个错误
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) { }
}
发布于 2021-12-21 18:12:29
您已经给出了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://stackoverflow.com/questions/70439831
复制相似问题