views:

1059

answers:

3

I have a MKMapView that has a number of annotations. Selecting the pin displays the callout and pressing the accessory pops a new viewcontroller onto the stack. However when I press back from that new VC the callout is still open. How do I close it?

I have tried

if([[myMapView selectedAnnotations] count] > 0)
{
    //deselect that annotation
    [myMapView deselectAnnotation:[[myMapView selectedAnnotations] objectAtIndex:0] animated:NO];
}

but this does not work. The selectedAnnotations does have a single entry in the array so it does go into this statement but the callout is not closed.

Do I need to add something to my MKAnnotation implementation or my MKPinAnnotationView?

A: 

When you reclick the pin the callout should go away...

Daniel
thanks, but I need to close it programmatically
joneswah
A: 

In lieu of a nice solution the following hacky approach works in the viewWillAppear:animated

    for( MyMapAnnotation *aMKAnn in  [myMapView annotations])
    {
     //dodgy select then deselect each annotation
     [myMapView selectAnnotation:aMKAnn animated:NO];
     [myMapView deselectAnnotation:aMKAnn animated:NO];
    }

the selectedAnnotations array does have 1 value but deselecting that value still did not close the call out? So I simply iterate through all annotations and select and deselect. I don't have many annotations so probably not too bad a performance hit?

I would appreciate an elegant solution if anyone has better ideas?

joneswah
+1  A: 

The objects in selectedAnnotations are instances of MKAnnotationView. the reference says that they are annotations but it's not true.

NSArray *selectedAnnotations = mapView.selectedAnnotations;
for(id annotationView in selectedAnnotations) {
    [mapView deselectAnnotation:[annotationView annotation] animated:NO];
}
af