mapView: didSelectAnnotationView: not functioning properly.

谁说我不能喝 提交于 2019-12-07 03:27:18

问题


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?


回答1:


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:




回答2:


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];



回答3:


- (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.



来源:https://stackoverflow.com/questions/26620672/mapview-didselectannotationview-not-functioning-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!