2011-10-28 25 views
12

Me preguntaba si alguien podría decirme cómo agregar un accesorio de leyenda derecha a una anotación de mapa. todo lo que intento no parece llegar a ninguna parte, por lo que cualquier ayuda sería apreciada.Right Callout Método de accesorio e implementación

EDITAR

me han tratado esta línea de código, pero nada diferente ocurre con la anotación.

- (MKAnnotationView *)mapview:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
MKAnnotationView *aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""]; 
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
aView.canShowCallout = YES; 
aView.annotation = annotation; 
return aView; 
} 
+0

puedo mostrar lo que intentaste y exactamente qué errores o problemas se obtiene? – Anna

Respuesta

34

El nombre del método es incorrecto. Debe ser mapView con un capital V:

- (MKAnnotationView *)mapView:(MKMapView *)sender 
      viewForAnnotation:(id <MKAnnotation>)annotation 

Objective-C entre mayúsculas y minúsculas.

Si el método aún no recibe una llamada, entonces el otro problema es que la vista de mapa delegate no está configurada. En el código, configúrelo en self o en Interface Builder adjunte el delegado al propietario del archivo.

También asegúrese de configurar el title de la anotación antes de agregarlo; de lo contrario, la leyenda aún no se mostrará.

Los cambios anteriores deben corregir el botón de accesorio que no aparece.


Algunas otras sugerencias no relacionadas ...

En viewForAnnotation, que deben apoyar vista de anotación reutilización llamando dequeueReusableAnnotationViewWithIdentifier:

- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    static NSString *reuseId = @"StandardPin"; 

    MKPinAnnotationView *aView = (MKPinAnnotationView *)[sender 
       dequeueReusableAnnotationViewWithIdentifier:reuseId]; 
    if (aView == nil) 
    { 
     aView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
        reuseIdentifier:reuseId] autorelease]; 
     aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     aView.canShowCallout = YES; 
    } 

    aView.annotation = annotation; 

    return aView; 
} 

Si su proyecto utiliza ARC, retire el autorelease.


Por cierto, para responder al accesorio pulse el botón, poner en práctica el método calloutAccessoryControlTapped delegado:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
     calloutAccessoryControlTapped:(UIControl *)control 
{ 
    NSLog(@"accessory button tapped for annotation %@", view.annotation); 
} 
Cuestiones relacionadas