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?
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:
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];
- (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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With