Search code examples
iosswiftmapkitseguemkannotation

Replace pushViewController with segue in MapKit


I created app with own MKAnnotation. I have another ViewController and I try send data by segue. Now, I have Navigation Controller and working code like this

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

But, I want replace Navigation Controller by segue. I created segue with identifier "pinTouched", and replace last line in my code by

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

My perform func look like this

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

I tried in few ways send this data in this function, but I have only white screen. How can I fix it?

Thanks for answer! :)


Solution

  • You need to fill the data

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

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