Tengo una vista de mapa con anotaciones, y estas anotaciones muestran una leyenda. Cuando se hace clic en el botón de detalle de divulgación de la leyenda, se dirige a una nueva vista.Anotaciones MKMapView cambiando/perdiendo el orden?
Mis MKAnnotaciones son una clase personalizada que implementa <MKAnnotation>
. Llamemos a esa clase MyClass. Se almacenan en un NSMutableArray. Durante viewdidload de esta vista, agrego cada objeto de MyClass en esta matriz a las anotaciones de la vista de mapa. Usando el depurador, puedo ver que una vez que se completa todo esto, el orden [de las anotaciones self.MapView] es el mismo que el NSMutableArray.
Ahora configuro otro punto de interrupción en mapView: viewForAnnotation: y miro el orden de 1) mi NSMutableArray y 2) [self.MapView annotations]. La matriz está, por supuesto, en el mismo orden. Sin embargo, el orden de las anotaciones ha sido codificado.
Esto fue un gran problema para mí, porque necesitaba usar la instancia específica de MyClass que el usuario seleccionó en la siguiente vista. AKA, quería ver la anotación, encontrar su índice y luego usar eso para obtener el mismo índice dentro de la matriz.
Ahora me he dado cuenta de que puedo guardar la anotación directamente (proveniente de un fondo de Android, esto fue muy bueno para mí). Sin embargo, todavía estoy conceptualmente perdido en cuanto a por qué la orden se revolvió. ¿Alguien me puede ayudar? Código de abajo:
- (void)viewDidLoad
{
if([fromString isEqualToString:@"FromList"])
self.navigationItem.hidesBackButton = TRUE;
else {
self.navigationItem.rightBarButtonItem = nil;
}
self.array = [MySingleton getArray];
//set up map
//declare latitude and longitude of map center
CLLocationCoordinate2D center;
center.latitude = 45;
center.longitude = 45;
//declare span of map (height and width in degrees)
MKCoordinateSpan span;
span.latitudeDelta = .4;
span.longitudeDelta = .4;
//add center and span to a region,
//adjust the region to fit in the mapview
//and assign to mapview region
MKCoordinateRegion region;
region.center = center;
region.span = span;
MapView.region = [MapView regionThatFits:region];
for(MyClass *t in self.array){
[MapView addAnnotation:t];
}
[super viewDidLoad];
}
//this is the required method implementation for MKMapView annotations
- (MKAnnotationView *) mapView:(MKMapView *)thisMapView
viewForAnnotation:(MyClass *)annotation
{
static NSString *identifier = @"MyIdentifier";
//the result of the call is being cast (MKPinAnnotationView *) to the correct
//view class or else the compiler complains
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[thisMapView
dequeueReusableAnnotationViewWithIdentifier:identifier];
if(annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
annotationView.pinColor = MKPinAnnotationColorGreen;
//pin drops when it first appears
annotationView.animatesDrop=TRUE;
//tapping the pin produces a gray box which shows title and subtitle
annotationView.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = infoButton;
return annotationView;
}
Gracias por su último punto. Siempre aprendiendo –