tutorial objective iphone objective-c

iphone - objective c tutorial



Cómo cerrar una llamada para MKAnnotation en un MKMapView (6)

Cuando vuelva a pinchar el pin, la llamada debería desaparecer ...

Tengo un MKMapView que tiene varias anotaciones. Al seleccionar el pin, se muestra la llamada y al presionar el accesorio aparece un nuevo controlador de vista en la pila. Sin embargo, cuando presiono de nuevo desde ese nuevo VC, la llamada sigue abierta. ¿Cómo lo cierro?

Yo he tratado

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

Pero esto no funciona. Las Anotaciones seleccionadas tienen una sola entrada en la matriz, por lo que entra en esta declaración pero la llamada no se cierra.

¿Necesito agregar algo a mi implementación de MKAnnotation o mi MKPinAnnotationView?


En caso de que quiera seguir con la documentación del kit de mapas.

for (NSObject<MKAnnotation> *annotation in [mapView selectedAnnotations]) { [mapView deselectAnnotation:(id <MKAnnotation>)annotation animated:NO]; }


En lugar de una buena solución, el siguiente enfoque hacky funciona en viewWillAppear: animated

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

¿La matriz selectedAnnotations tiene 1 valor pero al deseleccionar ese valor todavía no se cerró la llamada? Así que simplemente itero todas las anotaciones y selecciono y deselecciono. No tengo muchas anotaciones, así que probablemente no sea un golpe de rendimiento tan malo.

¿Apreciaría una solución elegante si alguien tiene mejores ideas?


Los objetos en las anotaciones seleccionadas son instancias de MKAnnotation

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


func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { let pin = view.annotation mapView.deselectAnnotation(pin, animated: false) performSegueWithIdentifier("Next VC Segue", sender: nil) }

Anule la selección de la anotación justo antes de pasar al nuevo controlador de vista. De esa manera se irá cuando vuelvas.


- (void)deselectAllAnnotations { NSArray *selectedAnnotations = [self.mapViewObj.mapView selectedAnnotations]; for (int i = 0; i < [selectedAnnotations count]; i++) { [self.mapViewObj.mapView deselectAnnotation:[selectedAnnotations objectAtIndex:i] animated:NO]; } }

Esto puede ayudarte a resolver tu problema.