在 MapKit 中用 segue 替换 pushViewController

Replace pushViewController with segue in MapKit

我用自己的 MKAnnotation 创建了应用程序。我有另一个 ViewController,我尝试通过 segue 发送数据。 现在,我有了导航控制器和这样的工作代码

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

    let annotationView:MyAnnotation = view.annotation as! MyAnnotation
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let detailStoryboard = storyboard.instantiateViewController(withIdentifier: "stationDetails") as! DetailViewController
    detailStoryboard.stationTitleText = (annotationView.title)!
    detailStoryboard.bikeRacksText = (annotationView.bike_racks)!
    detailStoryboard.bikesText = (annotationView.bikes)!
    detailStoryboard.freeRacksText = (annotationView.free_racks)!
    detailStoryboard.distanceText = (annotationView.distance)!
    self.navigationController?.pushViewController(detailStoryboard, animated: true)
}

但是,我想用 segue 替换 Navigation Controller。 我使用标识符 "pinTouched" 创建了 segue,并将代码中的最后一行替换为

performSegue(withIdentifier: "pinTouched", sender: nil)

我的执行函数是这样的

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "pinTouched")
    {
        var detailScreen = segue.destination as! DetailViewController
    }
}

我尝试了几种方法在此函数中发送此数据,但我只有白屏。 我该如何解决?

感谢解答! :)

您需要填写数据

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
     performSegue(withIdentifier: "pinTouched", sender:view.annotation)
}

然后里面 prepare(for segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "pinTouched")
    {
         let detailStoryboard = segue.destination as! DetailViewController
         let annotationView = sender as! MyAnnotation 
         detailStoryboard.stationTitleText = (annotationView.title)!
         detailStoryboard.bikeRacksText = (annotationView.bike_racks)!
         detailStoryboard.bikesText = (annotationView.bikes)!
         detailStoryboard.freeRacksText = (annotationView.free_racks)!
         detailStoryboard.distanceText = (annotationView.distance)!
    }
}