1

I'm working on a map which have some markers and I want to create an info view which will show an image and some text for each marker.

map

So, when I press the info button, it goes to a info view.

map2

I have this on the code:

func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == annotationView.rightCalloutAccessoryView {

        performSegueWithIdentifier("showInfo", sender: data)

    }
}

But I want to pass the data from "locations" so I could show a image for each.

I need to do this in Swift.

Thanks a lot.

Tárraga
  • 461
  • 1
  • 6
  • 13
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Ujjwal Khatri Mar 30 '16 at 09:57

3 Answers3

4

You should add this method to your class if you do not have and then pass your data to destination view's data.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showInfo" {
        let destination = segue.destinationViewController as! YourViewController
        //this will execute before next ViewController's viewDidLoad method
        destination.data = sender
    }
}
Mirsat Murutoglu
  • 202
  • 2
  • 11
0

You have to override prepareForSegue function in your viewController:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if "showInfo" == segue.identifier {
           // Then pass your data
           let destController = segue.mapViewController as! YourViewController 
           destController.yourData = sender
        }
    }
Thomas
  • 1,289
  • 1
  • 17
  • 29
0

Connect the info button outlet to your viewcontroller. Then create func as follows:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if "YourSegue" == segue.identifier {
        // Then pass your data
    }
}
honk
  • 9,137
  • 11
  • 75
  • 83
gjain
  • 1
  • 2