在calloutAccessoryControlTapped

时间:2019-10-23 00:42:27

标签: swift uikit mapkit swiftui

正在构建一个主要是SwiftUI应用程序...我正在尝试将父视图中BindableObject的值连接(绑定)到MapKit子视图,以便在点击注释(标注附件)时(小( i)注释标签上的按钮...

enter image description here

...将$ showDetails的值更改为true,将其连接到视图层次结构上方的“ sheet(isPresented:$ showDetails ...)”,然后显示模式:

enter image description here

struct MapView: UIViewRepresentable {

    @Binding var mapSelected: Int
    @Binding var showDetails: Bool    // I WANT TO TOGGLE THIS WHEN CALLOUT ACCESSORY IS TAPPED

    @Binding var location: Location
    @Binding var previousLocation: Location

    @Binding var autoZoom: Bool
    @Binding var autoZoomLevel: Int

    class Coordinator: NSObject, MKMapViewDelegate {

        @Binding var showDetails: Bool

        init(showDetails: Binding<Bool>) {
            _showDetails = showDetails
        }

        func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
            guard let coordinates = view.annotation?.coordinate else { return }
            let span = mapView.region.span
            let region = MKCoordinateRegion(center: coordinates, span: span)
            mapView.setRegion(region, animated: true)
        }

        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            guard let annotation = annotation as? LocationAnnotation else { return nil }
            let identifier = "Annotation"
            var annotationView: MKMarkerAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView
            if annotationView == nil {
                annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                annotationView?.markerTintColor = UIColor(hex: "#00b4ffff")
                annotationView?.animatesWhenAdded = true
                annotationView?.canShowCallout = true
                annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            } else {
                annotationView?.annotation = annotation
            }
            return annotationView
        }

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
            guard let loc = view.annotation as? LocationAnnotation else {
                    print("sorry")
                    return
            }
            print(self.showDetails)     // true, false, true, false, ...
            self.showDetails.toggle()   // THIS DOES TOGGLE, BUT THE VALUE IS NOT OBSERVED BY THE PARENT VIEW
        }
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator(showDetails: $showDetails)
    }

    func makeUIView(context: Context) -> MKMapView {
            let map = MKMapView()
            map.delegate = context.coordinator
            return map
    }

    func updateUIView(_ uiView: MKMapView, context: Context) {
            switch mapSelected {
                    case 0:
                            uiView.mapType = .standard
                    default:
                            uiView.mapType = .hybrid
            }
            let currentRegion = uiView.region  // get the current region
            var span: MKCoordinateSpan
            var center: CLLocationCoordinate2D
            var newRegion: MKCoordinateRegion

            if currentRegion.span.latitudeDelta == 90.0 && currentRegion.span.longitudeDelta == 180.0 {   // INITIAL
                    span = MKCoordinateSpan(latitudeDelta: 18.0, longitudeDelta: 18.0)
                    center = CLLocationCoordinate2D(latitude: 54.5, longitude: -110)
                    newRegion = MKCoordinateRegion(center: center, span: span)
                    uiView.setRegion(newRegion, animated: true)
            }
            updateAnnotations(from: uiView)
    }

   // ...
}

我期望的是,当点击标注附件时,会切换showDetails(它是),但是在父视图中看不到任何效果-不会显示工作表。似乎绑定没有发布其新状态。

我想念/做错了什么?我发现将UIKit与SwiftUI集成起来很容易,然后再困难,然后又容易,然后再不可能。请帮忙!

1 个答案:

答案 0 :(得分:0)

事实证明,我在找错地方了。问题不在于上面的代码(可以100%正确),而是在容器视图中,应该一直在监听 showDetails 的更改值,但是没有不是因为我将showDetails传递给它的方式,例如

ContentView.swift

Footer(search: search,
    locationStore: self.locationStore,
    searchCoordinates: self.searchCoordinates,
    showDetails: self.showDetails) // NOT PASSED AS A BINDING...

Footer.swift

struct Footer: View {

@EnvironmentObject var settingsStore: SettingsStore

@ObservedObject var search: SearchTerm
@ObservedObject var locationStore: LocationStore
@ObservedObject var searchCoordinates: SearchCoordinates

@State var showDetails: Bool   // DECLARE LOCAL STATE, WILL NOT BE AWARE OF CHANGE TO showDetails FROM MapView

非常简单的解决方法:

ContentView.swift

Footer(search: search,
    locationStore: self.locationStore,
    searchCoordinates: self.searchCoordinates,
    showDetails: self.$showDetails)

Footer.swift

struct Footer: View {

@EnvironmentObject var settingsStore: SettingsStore

@ObservedObject var search: SearchTerm
@ObservedObject var locationStore: LocationStore
@ObservedObject var searchCoordinates: SearchCoordinates

@Binding var showDetails: Bool  

当在传入的变量中意外使用错误的属性包装器时,感觉像这样的错误很容易在SwiftUI中遇到(真棒,顺便说一句)。编译器不会警告您,也没有运行时错误,只是...什么都没有发生。和您一样,您可能会倾向于追逐红色鲱鱼,也就是完美的代码。