2010-02-24 9 views
6

Tamam, bu yüzden bir MKMapView içinde açıklanmasına izin vermek istediğiniz X nesnesine sahipsiniz. en temiz çözüm nedirHangi MKAnnotation'a dokunulduğunu bilmek için temiz çözüm?

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

: Bazı çizgisi aday geçtiğinde, içeride olayı işlemek

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

Sonra

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

Ve içeride açıklama görünümü oluşturmak: Bu şekilde yapmak X en son musluk etkinliğine geçmek için?

cevap

17

Sorunuzu anlıyorsanız DDAnnotation sınıfınıza bir referans veya özellik eklemeniz gerekir, böylece calloutAccessoryControlTapped yönteminizde nesneye erişebilirsiniz. Sonra

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

: Eğer ek açıklama oluşturduğunuzda

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

Bunu yaptım ve

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

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

Teşekkürler! MKAnnotationView arayüzünün ek açıklama özelliğini görmedim! Aradığım şey bu! –

0

o tamam çalıştı!

Tam olarak ihtiyacım olan şey, çünkü haritaya dokunduğunda bir şey yapmam gerekiyordu, ancak açıklama normal olarak ek açıklama akışının içine giriyordu.

- (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 
    } 
} 
İlgili konular