w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Swift Annotation CallOut Button selection

In the calloutAccessoryControlTapped delegate method, self is not the annotation whose callout accessory view was tapped.

self refers to the instance of the class the method is contained in (i.e. an instance of ViewController). The other issue is that in the println, you put the variable reference outside the quotes instead of inside.


The delegate method gives you a reference to the annotation view: the annotationView argument.

The annotation view contains a reference to the annotation: annotationView.annotation.


However, please note your delegate method declaration differs slightly from the documented one (annotationView argument has a local name view and other arguments are marked as optional with the ! suffix).

It's better to use the documented declaration even though your version will technically still work.

Complete example below also shows how to check if the annotation is one of your custom objects and how to access the custom property:

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

    if control == view.rightCalloutAccessoryView {
        println("Disclosure Pressed! (view.annotation.subtitle)")

        if let cpa = view.annotation as? CustomPointAnnotation {
            println("cpa.imageName = (cpa.imageName)")
        }
    }

}


Side note:
Since you've set the leftCalloutAccessoryView to a UIImageView, tapping on it will not call calloutAccessoryControlTapped because it is only called for views that are subclasses of UIControl. UIImageView is not a subclass of UIControl. If you need to detect taps on the left accessory, create a custom UIButton (and set its image) instead of a UIImageView.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.