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



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
}

这是我正在从事的一个旧项目的摘录。当点击注释上的信息附件时,它会打开一个新的表视图控制器。我做了一个名为 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
}
}  

最新更新