Swift 将注释添加到地图并打开视图

Swift 将注释添加到地图并打开视图,swift,xcode,mapkit,Swift,Xcode,Mapkit,我在整个互联网上都搜索过了,并没有找到任何解决我这个(看起来)简单问题的方法。我有一个地图,用户可以在点击地图时向地图添加注释。以下是我的设置: // Tap to set location @IBAction func mapPoint(_ sender: UITapGestureRecognizer) { // Retrieve coordinates let location = sender.location(in: self.mapView) let locC

我在整个互联网上都搜索过了,并没有找到任何解决我这个(看起来)简单问题的方法。我有一个地图,用户可以在点击地图时向地图添加注释。以下是我的设置:

// Tap to set location
@IBAction func mapPoint(_ sender: UITapGestureRecognizer) {

    // Retrieve coordinates
    let location = sender.location(in: self.mapView)
    let locCoord = self.mapView.convert(location, toCoordinateFrom: self.mapView)

    // Create pin
    let annotation = MKPointAnnotation()
    annotation.coordinate = locCoord
    annotation.title = "Meeting Point"

    self.mapView.addAnnotation(annotation)

    func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {

        // Open view here
        let storyBoard: UIStoryboard = UIStoryboard(name: "Map", bundle: nil)
        let secondVC = storyBoard.instantiateViewController(withIdentifier: "secondVC") as! MapControllerMeet
        self.present(secondVC, animated: true, completion: nil)

    }

}

当用户添加注释时,我希望在地图上以模式打开一个新视图,换句话说,我希望为我的手势识别器指定一个动作+一个序列。这可能吗?有什么想法吗?

你可以创建一个背景透明的VC,给它id并调用这个函数作为手势的结束

func showModal() {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "popUpId") as! popUpViewController 
    vc.providesPresentationContextTransitionStyle = true
    vc.definesPresentationContext = true 
    vc.modalPresentationStyle = .overCurrentContext
    self.present(vc, animated: false, completion: nil)
}

您可以使用
mapView:didAddAnnotationViews:
。添加注释视图后将调用此方法

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    // Open view here

    // You can also get added annotation
    let annotationView = views.first
    print(annotationView?.annotation?.title)
}

谢谢你的回复。我已经实现了这个功能,但是第二个VC仍然没有显示,只有pin显示。如果你在手机上。overCurrentContext是一个全屏显示,所以它不会有任何区别,除非你在iPad上运行它。在这种情况下,控制器看起来应该可以工作。我对编码tho相当陌生,对于如何在我的代码中实现这些信息有点迷茫。你能举个例子说明怎么做吗?我的评论是一个链接,点击并阅读,当你遇到问题时回到这里嗨,我试过了,但仍然不起作用。我在我的原始帖子中编辑了代码,你能检查一下,看看我错了什么吗?你说它不工作是什么意思?你有错误吗?