5
votes

My iPhone app has a mapview with a large number of locations that the user can select from. I would like him to be able to tap on one of the annotations to display its callout view, and then again to actually select it. The problem is that the didSelectAnnotationView only gets called once.

So how can I detect the selection an already selected annotation? Alternatively, how can I deselect an annotation without hiding the callout view? The user can work round this by deselecting the annotation before he selects it again, but this is not intuitive, and I want to avoid him having to do this.

3
If you are using a subclass of MKAnnotationView can you not detect the second press on that view in your own code? (rather than on the 'pin')Diziet
I am not currently subclassing MKAnnotationView, though I guess I could. But I am surprised that there does not seem to be a simple solution to this.Philip Sheard
I've just had a fiddle around and I see what you're getting at more clearly now. I click on the annotation and it vanishes though, so no chance of a second click without reselecting the relevant pin anyway.Diziet
Could you not use the typical approach of adding a button as the rightCalloutAccessoryView and handle the button press in calloutAccessoryControlTapped?user467105
That would be the obvious solution, nice username by the way.Diziet

3 Answers

8
votes

I just found the solution with the code below:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    [mapview deselectAnnotation:view.annotation animated:NO];

}

This way the selected annotation gets deselected and you can select it once again.

4
votes

For those who are having this problem in '16, here is the Swift version:

mapView.deselectAnnotation(view.annotation!, animated: false)
0
votes

The annotation can't move from the selected state to the selected state again.

You can instead use a custom mkannotationview, where you override setSelected, and install a UITapGestureRdcognizer on your view, and remove it when the view is deselected.

wire up the recognized to do what you want for the tap-when-selected state.