3

I have a map with annotations, after clicking the pin, the callout shows with title of annotation and a disclosure button. When I tap button the segue is triggered and I move to another view. How to determine what annotation was clicked, or pass the title to another view.

func mapView(mapView: MKMapView!,
    viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if annotation is MKUserLocation{
            return nil
        }

        let reuseId = "pin"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

        if(pinView == nil){
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.pinColor = .Red

            var calloutButton = UIButton.buttonWithType(.DetailDisclosure) as UIButton
            pinView!.rightCalloutAccessoryView = calloutButton
        } else {
            pinView!.annotation = annotation
        }
        return pinView!
}

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

    if control == annotationView.rightCalloutAccessoryView {
        performSegueWithIdentifier("Detail", sender: self)
    }
}

Thanks

Leszek
  • 71
  • 1
  • 7
  • In calloutAccessoryControlTapped, annotationView.annotation will give you the annotation object. When calling performSegue, set sender to annotationView instead of self. See http://stackoverflow.com/questions/14805954/mkannotationview-push-to-view-controller-when-detaildesclosure-button-is-clicked for an example (it's in Objective-C but shouldn't be too difficult to convert to Swift). I do not recommend the tag approach. –  Oct 24 '14 at 14:25

2 Answers2

0

You can use MKAnnotation title of annotation property for finding the pins if annotation title is different

 annotationView.annotation.title

return String.Compare String to differenciate.

or

Use tag property in pinview

pinView!.tag //set tag for each pin in `viewForAnnotation` method

In

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

    //get tag here
      if(annotationView.tag == 0){
        //Do for 0 pin
     }

    if control == annotationView.rightCalloutAccessoryView {
        performSegueWithIdentifier("Detail", sender: self)
    }
}
codester
  • 34,623
  • 10
  • 72
  • 70
-1

Try saving the title to you the NSUserDefaults then grabbing that object in the new view. This is the method I use.

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


        println(view.annotation.title) // annotation's title

        let title = view.annotation.title


        NSUserDefaults.standardUserDefaults().setObject(title, forKey: "Title")


        var InfoView = self.storyboard?.instantiateViewControllerWithIdentifier("Spot") as! UIViewController

Once you have saved the title to the NSUserDefaults you can simply grab the object in the new "InfoView or whatever you are calling it" like this let spotTitle = NSUserDefaults.standardUserDefaults().objectForKey("SpotTitle") as! String

Hope this helps

Trip Phillips
  • 420
  • 5
  • 17