Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapView: didSelectAnnotationView: not functioning properly.

I'm building an IOS app that uses the built in map view. Im successfully placing custom annotations, etc. However, I'm having a problem with the delegate function that is called when an annotation is pressed (mapView:didSelectAnnotationView).

The first time I press an annotation, the function is called properly. However, if I proceed to click the same annotation again, the function does not fire. If I click on a different annotation at this point, the function WILL fire but then if I click on THAT annotation again, the function does not fire. Basically, I can never click on the same annotation twice in a row. The delegate function will only be called the first time. Has anyone encountered this problem? Is there somewhere in particular I should look for the bug?

like image 528
Nate Avatar asked Oct 29 '14 00:10

Nate


3 Answers

Well, when you think about it, you have already selected that annotation view. It doesn't make sense for the delegate to tell you that the pin is selected if it already is.

A simple fix could be to set the annotation to deselected in the delegate call. This should allow you to get the call again.

[annotation setSelected:NO animated:NO];

Vists here for the method you need to call. https://developer.apple.com/library/ios/documentation/mapkit/reference/MKAnnotationView_Class/index.html#//apple_ref/occ/instm/MKAnnotationView/setSelected:animated:

like image 89
Bergasms Avatar answered Nov 05 '22 19:11

Bergasms


Friend suggested an idea and it turned out to be correct. 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.

[mapView deselectAnnotation:view.annotation animated:false];
like image 33
Nate Avatar answered Nov 05 '22 20:11

Nate


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

    }
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)aView
    {
    }

I hope this will work for you :) I have faced the same problem, this code worked for me.

like image 1
Nikita Khandelwal Avatar answered Nov 05 '22 20:11

Nikita Khandelwal