ios - SwiftUI MapKit UIViewRepresentable 无法显示警报

标签 ios xcode mapkit swiftui uiviewrepresentable

我有一个 SwiftUI 应用程序,其中有几个 View 是使用 MapKit 制作的 map UIViewRepresentable。我有兴趣点和使用的自定义注释 用于进一步操作的左右标注按钮。在右边我只是想要 显示有关航路点的信息。在左侧,我想发出警报 进一步操作的选择 - 例如,插入一个新的注释点。前 SwiftUI 我刚刚发出警报并执行了上述两项操作。但据我所知, UIViewRepresentable 版本上没有 self.present。因此我没有去过 能够显示警报。

只是为了实验 - 我将 SwiftUI 警报代码附加到 SwiftUI View 调用 map View 。使用 Observable bool 值我确实可以引发这两个警报。 这对我来说似乎很奇怪,但也许是绑定(bind)到全局属性的警报代码 可以在任何地方。

我的第一次尝试:(结构是 DetailMapView)

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if control == view.leftCalloutAccessoryView {
        guard let tappedLocationCoord = view.annotation?.coordinate else {return}
        let tappedLocation = CLLocation(latitude: tappedLocationCoord.latitude, longitude: tappedLocationCoord.longitude)

        let ac = UIAlertController(title: nil, message: nil, preferredStyle: .alert)

        let deleteAction = UIAlertAction(title: "Delete Waypoint", style: .destructive) { (action) in
            //some stuff
        }

        let insertAction = UIAlertAction(title: "Insert Waypoint After This", style: .default) { (action) in
            //some stuff
        }

        let cancelAction = UIAlertAction(title: "Cancel", style: .default) { (action) in
            //some stuff
        }//cancelAction

        ac.addAction(deleteAction)
        ac.addAction(insertAction)
        ac.addAction(cancelAction)

        //tried adding self, referencing parent - always error saying
        //the object does not have a .present
        mapView.present(ac, animated: true)

    } else if control == view.rightCalloutAccessoryView {
        //more of the same
    }
}//annotationView

然后我删除了警报代码并添加了:

parent.userDefaultsManager.shouldShowAnnotationEditMenu.toggle()

我将通话屏幕更改为:

@ObservedObject var userDefaultsManager: UserDefaultsManager
var aTrip: Trip?

var body: some View {

    VStack {
        Text(aTrip?.name ?? "Unknown Map Name")
            .padding(.top, -50)
            .padding(.bottom, -20)

        DetailMapView(aTrip: aTrip, userDefaultsManager: userDefaultsManager)
            .padding(.top, -20)
            .alert(isPresented: $userDefaultsManager.shouldShowAddress) {
                //Alert(title: Text("\(aTrip?.name ?? "No") Address"),
                Alert(title: Text(self.userDefaultsManager.approximateAddress),
                      message: Text("This is the approximate street address."),
                      dismissButton: .default(Text("Got it!")))
            }//.alert shouldShowAddress

        Text("This is the view where the trip information will be displayed.")
            .multilineTextAlignment(.center)
        .alert(isPresented: $userDefaultsManager.shouldShowAnnotationEditMenu) {
            Alert(title: Text("Edit Annotations"),
                  message: Text("Choose this to insert an Annotation."),
                  dismissButton: .default(Text("Got it!")))
        }//.alert shouldShowAddress
    }
}

我想如果这安全的话我可以让它工作——但它看起来比它更复杂 应该是。

思路是这样的:

enter image description here

任何指导将不胜感激:Xcode 版本 11.3.1 (11C504)

最佳答案

我为此花了一个小时,我是 SwiftUI 的新手,我跳进去只是为了回答一些简单的问题。

一种方法是使用 Bool(@State@Binding)。

您还需要有一个View,而不是直接在您的SceneDelegate 中使用您的UIViewRepresentable。因为那是您链接警报的地方。

像这样:

struct MainView: View {
    @State var text = ""
    @State var showingAlert: Bool

    var body: some View {
        VStack {
            MapView(showingAlert: self.$showingAlert)
                .alert(isPresented: $showingAlert) { () -> Alert in
                    print("SHOWING ALERT BODY: --> \($showingAlert.wrappedValue)")
                    return Alert(title: Text("Important message"), message: Text("Go out and have a girlfriend!"), dismissButton: .default(Text("Got it!")))
            }
        }
    }
}

然后你的 MapView 应该是这样的:

struct MapView: UIViewRepresentable {

    let landmarks = LandmarkAnnotation.requestMockData()

    @Binding var showingAlert: Bool

    func makeCoordinator() -> MapViewCoordinator {
        MapViewCoordinator(mapView: self, showingAlert: self.$showingAlert)
    }

    /**
     - Description - Replace the body with a make UIView(context:) method that creates and return an empty MKMapView
     */
    func makeUIView(context: Context) -> MKMapView {
        MKMapView(frame: .zero)
    }

    func updateUIView(_ view: MKMapView, context: Context){
        //If you changing the Map Annotation then you have to remove old Annotations
        //mapView.removeAnnotations(mapView.annotations)
        view.delegate = context.coordinator
        view.addAnnotations(landmarks)
    }
}

struct MapView_Previews: PreviewProvider {
    static var previews: some View {
        MapView(showingAlert: Binding<Bool>.constant(true))
    }
}

最后,在您的 MapViewCoordinator 中(我想您有这个类,这是实现 MKMapViewDelegate 的委托(delegate)方法的类。)。

/*
 Coordinator for using UIKit inside SwiftUI.
 */
class MapViewCoordinator: NSObject, MKMapViewDelegate {

    var mapViewController: MapView!
    @Binding var showAlert: Bool

    init(mapView: MapView, showingAlert: Binding<Bool>) {

        self.mapViewController = mapView
        self._showAlert = showingAlert
        super.init()
    }

    func mapView(_ mapView: MKMapView, viewFor
        annotation: MKAnnotation) -> MKAnnotationView?{
        //Custom View for Annotation
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customView")
        annotationView.canShowCallout = true
        //Your custom image icon
        annotationView.image = UIImage(named: "locationPin")
        return annotationView
    }

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        print("calloutAccessoryControlTapped")
    }

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        print("didSelect")

        self.showAlert = true
    }
}

如您所见,我只使用了 Bool 标志。特别是 @Binding 一个。

enter image description here

关于ios - SwiftUI MapKit UIViewRepresentable 无法显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60348668/

相关文章:

xcode - 哪种文件是有效的Xcode行为脚本?

ios - 从底部看 Mapkit 自定义标注幻灯片 View

ios - 具有mbtiles数据库的iOS 7中的MKTileOverlay

ios - 为什么在UIWebView上播放视频时会调用setMessageLoggingBlock :?

ios - 如何在 View 之间实现滑动/滑动动画?

swift - 应用程序在显示另一个窗口时崩溃,但仅在不调试时崩溃

iphone - 如果我不再使用 MKMapView,为什么在释放它后我会崩溃?

android - 如何知道Web SQL已经存在?

objective-c - 将文档文件夹中的数据显示到 UITableView 中,然后转到 Objective C 中的选择

ios - TableView 内容未显示