Search code examples
iosmkmapviewmkannotationview

mapView:didSelectAnnotationView gets fired only once


I have a MKMapView, I display one MKAnnotaionView on top of it. My problem is when I click on the AnnotationView the first time, mapView:didSelectAnnotationView gets called, but if I click on it again nothing happens. Why is this happening?


Solution

  • - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView
        {
            indexPathTag=aView.tag;
            [mapView deselectAnnotation:aView.annotation animated:YES];
    
        }
    - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)aView
        {
        }
    

    When didSelectAnnotationView fires, it actually tags the annotation as selected somehow. Then when you click it again, the delegate function doesn't fire because it is 'already selected'. You have to manually deselect the annotation by calling the following function once you're done doing what you want. So you have to Add [mapView deselectAnnotation:aView.annotation animated:YES]; in didSelectAnnotationView delegate method.