gamerChristian
gamerChristian

Reputation: 481

Get MKPointAnnotation title

I've created a MKMapView which contains several MKPointAnnotations on the map. When the user clicks the UIButton in the view I would like to print the title in the log. How can I do that? So far I have this:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        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 = .Purple


        var rightButton: AnyObject! = UIButton.buttonWithType(UIButtonType.DetailDisclosure)
        //MapPointAnnotation *point = (MapPointAnnotation*)pinView.annotation;
        //rightButton.venue = point.venue;
        rightButton.titleForState(UIControlState.Normal)

        rightButton.addTarget(self, action: "rightButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
        pinView!.rightCalloutAccessoryView = rightButton as UIView

    }
    else {
        pinView!.annotation = annotation
    }

    return pinView
}



func rightButtonTapped(sender: AnyObject) {


}

Upvotes: 0

Views: 1700

Answers (1)

user467105
user467105

Reputation:

In the custom rightButtonTapped method, an easy and reliable way to get a reference to the annotation that was tapped is to use the map view's selectedAnnotations array:

func rightButtonTapped(sender: AnyObject) {
    if self.mapView.selectedAnnotations?.count == 0 {
        //no annotation selected
        return;
    }

    if let ann = self.mapView.selectedAnnotations[0] as? MKAnnotation {
        println("\(ann.title!)")
    }
}

(Even though selectedAnnotations is an array, the map view only allows one annotation to be "selected" at a time so the currently selected annotation is always at index 0.)


However, a better way than using a custom button method is to use the map view's calloutAccessoryControlTapped delegate method. The delegate method passes you a reference to the annotation view that was tapped from which you can easily get the underlying annotation.

To use the delegate method, remove the addTarget line for your custom method:

//Do NOT call addTarget if you want to use the calloutAccessoryControlTapped
//delegate method instead of a custom button method.
//rightButton.addTarget(self, action: "rightButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)

and then implement the delegate method instead of your custom button method:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    println("\(view.annotation.title!)")
}

Upvotes: 1

Related Questions