在MapKit中用segue替换pushViewController

时间:2018-08-29 14:34:39

标签: ios swift mapkit segue mkannotation

我用自己的MKA注释创建了应用。我有另一个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
    }
}

我尝试了几种方法通过此功能发送此数据,但是我只有白屏。 我该如何解决?

感谢您的回答! :)

1 个答案:

答案 0 :(得分:1)

您需要填写数据

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)!
    }
}