Swift - 按下MKAnnotation按钮时如何触发segue?

时间:2018-06-11 21:42:47

标签: ios swift mapkit segue mkannotation

Swift-Newbie问题:

我有两个视图控制器:我的家用VC使用MapKit,第二个用于MapDetailViewController'显示其他信息。

在家庭VC上,我添加了一个带有注释的兴趣点,以便在用户点击图钉时显示点和副标题的名称。当用户点击地图注释时,如何才能转到我的MapDetailViewController?

// Map view delegate which handles the annotation view
    extension ViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, viewFor annotation: 
        MKAnnotation) -> 

MKAnnotationView? {

    guard let annotation = annotation as? Artwork else { return nil }

    let identifier = "marker"

    var view: MKMarkerAnnotationView

    if let dequeuedView = 
mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? 
MKMarkerAnnotationView {

        dequeuedView.annotation = annotation

        view = dequeuedView

    } else {

        view = MKMarkerAnnotationView(annotation: annotation, 
reuseIdentifier: identifier)

        view.canShowCallout = true

        view.calloutOffset = CGPoint(x: -5, y: 5)

        // Broke up the button initialisation so that it has unique id 'button'
        let button = UIButton(type: .detailDisclosure)

        view.rightCalloutAccessoryView = button

        // Make the segue happen - tried to make it happen when object 'button' is pressed

        performSegue(withIdentifier: "MapDetail", sender: button)

    }

    return view
}

1 个答案:

答案 0 :(得分:0)

这是我正在处理的旧项目的摘录。当在注释上点击信息附件时,它会打开一个新的tableview控制器。我有一个名为Annotation的自定义类,它包含有关注释的所有信息。我不确定这是否会有所帮助:)

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

        let annotation = view.annotation as! Annotation
        let event = annotation.event

        if control == view.rightCalloutAccessoryView {
            let destination = self.storyboard!.instantiateViewController(withIdentifier: "EventViewController") as! EventViewController
            self.navigationController!.pushViewController(destination, animated: true)
            destination.event = event
    }
}  
相关问题