3
votes

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

2
In calloutAccessoryControlTapped, annotationView.annotation will give you the annotation object. When calling performSegue, set sender to annotationView instead of self. See stackoverflow.com/questions/14805954/… 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.user467105

2 Answers

0
votes

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

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