2010-02-24 8 views
6

Ok, por lo que normalmente tiene un objeto X que desea anotar dentro de un MKMapView. Esto se hace así:¿Solución limpia para saber qué MKAnnotation se ha aprovechado?

DDAnnotation *annotation = [[DDAnnotation alloc] initWithCoordinate: poi.geoLocation.coordinate title: @"My Annotation"]; 
[_mapView addAnnotation: annotation]; 

Luego de crear la vista de anotación en el interior

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

Y cuando alguna llamada se dio un golpecito, que controla el evento en el interior:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control; 

¿Cuál es la solución más limpia pasar X al último evento tap?

Respuesta

17

Si entiendo su pregunta, debe agregar una referencia o propiedad a su clase DDAnnotation para que en su método calloutAccessoryControlTapped pueda acceder al objeto.

@interface DDAnnotation : NSObject <MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    id objectX; 
} 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic, retain) id objectX; 

Cuando se crea la anotación:

DDAnnotation *annotation = [[DDAnnotation alloc] initWithCoordinate:poi.geoLocation.coordinate title: @"My Annotation"]; 
annotation.objectX = objectX; 
[_mapView addAnnotation: annotation]; 

continuación:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{ 

    DDAnnotation *anno = view.annotation; 
    //access object via 
    [anno.objectX callSomeMethod]; 
} 
+0

Gracias! ¡No noté la propiedad de anotación de la interfaz MKAnnotationView! ¡Eso es lo que estaba buscando! –

0

lo hice y funcionó bien!

Es exactamente lo que necesito porque necesitaba hacer algo cuando se tocaba el mapa pero dejaba que el toque en la anotación fluyera normalmente.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIGestureRecognizer *g = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)] autorelease]; 
    g.cancelsTouchesInView = NO; 
    [self.mapView addGestureRecognizer:g]; 

} 

- (void) handleGesture:(UIGestureRecognizer*)g{ 
    if(g.state == UIGestureRecognizerStateEnded){ 
     NSSet *visibleAnnotations = [self.mapView annotationsInMapRect:self.mapView.visibleMapRect]; 
     for (id<MKAnnotation> annotation in visibleAnnotations.allObjects){ 
      UIView *av = [self.mapView viewForAnnotation:annotation]; 
      CGPoint point = [g locationInView:av]; 
      if([av pointInside:point withEvent:nil]){ 
       // do what you wanna do when Annotation View has been tapped! 
       return; 
      } 
     } 
     //do what you wanna do when map is tapped 
    } 
} 
Cuestiones relacionadas