2010-08-30 56 views
10

Implementé una anotación personalizada derivada de MKAnnotation llamada ContainerAnnotation y una vista de anotación personalizada derivada de MKAnnotationView con un método drawRect: ContainerAnnotationView. Por alguna razón, el método drawRect: no se está llamando y no puedo entender por qué.MKAnnotationView drawRect: no se llama

Aquí está el código fuente para mi vista de anotación.

ContainerAnnotationView.h:

@interface ContainerAnnotationView : MKAnnotationView 
{ 
} 

@end 

ContainerAnnotationView.m:

@implementation ContainerAnnotationView 

- (void) drawRect: (CGRect) rect 
{ 
    // Draw the background image. 
    UIImage * backgroundImage = [UIImage imageNamed: @"container_flag_large.png"]; 
    CGRect annotationRectangle = CGRectMake(0.0f, 0.0f, backgroundImage.size.width, backgroundImage.size.height); 
    [backgroundImage drawInRect: annotationRectangle]; 

    // Draw the number of annotations. 
    [[UIColor whiteColor] set]; 
    UIFont * font = [UIFont systemFontOfSize: [UIFont smallSystemFontSize]]; 
    CGPoint point = CGPointMake(2, 1); 
    ContainerAnnotation * containerAnnotation = (ContainerAnnotation *) [self annotation]; 
    NSString * text = [NSString stringWithFormat: @"%d", containerAnnotation.annotations.count]; 
    [text drawAtPoint: point withFont: font]; 
} 

@end 

Desde mi controlador de vista:

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

    if ([annotation isKindOfClass: [ContainerAnnotation class]]) 
    { 
     ContainerAnnotationView * annotationView = (ContainerAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier: _containerMapAnnotationId]; 
     if (annotationView == nil) 
     { 
      annotationView = [[[ContainerAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: _containerMapAnnotationId] autorelease];  
      annotationView.centerOffset = CGPointMake(0, -17.5); 
      annotationView.rightCalloutAccessoryView = [UIButton buttonWithType: UIButtonTypeDetailDisclosure]; 
      annotationView.canShowCallout = YES; 
     } 
     annotationView.annotation = annotation; 

     return annotationView; 
    } 
    // etc... 
} 

tengo otras anotaciones que utilizan una v Anilla MKAnnotación con una imagen que funciona bien. También tengo otra vista de anotación personalizada que no implementa drawRect: funciona bien. ¿Alguna idea de lo que estoy haciendo mal aquí?

Respuesta

22

El problema resultó ser que mi drawRect: método no estaba siendo llamado debido a que el marco no se ajusta a un tamaño distinto de cero. Agregar un método initWithAnnotation: para hacer esto solucionó el problema.

- (id) initWithAnnotation: (id <MKAnnotation>) annotation reuseIdentifier: (NSString *) reuseIdentifier 
{ 
    self = [super initWithAnnotation: annotation reuseIdentifier: reuseIdentifier]; 
    if (self != nil) 
    { 
     self.frame = CGRectMake(0, 0, 30, 30); 
     self.opaque = NO; 
    } 
    return self; 
} 
2

¿Has llamado a setNeedsDisplay para esta vista subclase en cualquier lugar? (Sólo después de realizar esta visión visible es un buen lugar.)

+0

¿Cómo se hace visible la vista? ¿Se hace visible después de mapView: viewForAnnotation: returns? –

+0

Lo pido porque no veo ningún código para las otras vistas de anotación que explícitamente los hacen visibles. En ninguna parte llaman setNeedsDisplay. –

+0

Esto es todo lo que necesitaba para hacerlo funcionar. – VaporwareWolf