2010-02-24 15 views
6

Ok, vì vậy bạn thường có một số đối tượng X bạn muốn được chú thích bên trong một MKMapView. Bạn làm theo cách này:Giải pháp sạch để biết MKAnnotation nào đã được khai thác?

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

Sau đó, bạn tạo ra cái nhìn chú thích bên

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

Và khi một số lời thoại được khai thác, bạn xử lý các sự kiện bên trong:

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

giải pháp sạch nhất là gì để vượt qua X xuống sự kiện nhấn mới nhất?

Trả lời

17

Nếu tôi hiểu câu hỏi của bạn, bạn nên thêm tham chiếu hoặc thuộc tính vào lớp DDAnnotation của bạn để trong phương thức calloutAccessoryControlTapped bạn có thể truy cập đối tượng.

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

Khi bạn tạo các chú thích:

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

Sau đó:

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

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

Cảm ơn! Tôi không chú ý đến thuộc tính chú thích của giao diện MKAnnotationView! Đó là những gì tôi đang tìm kiếm! –

0

Tôi đã làm điều này và nó đã làm việc ổn!

Đó chính xác là những gì tôi cần vì tôi cần làm một điều gì đó khi bản đồ được khai thác nhưng cho phép nhấn vào luồng chú thích một cách bình thường.

- (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 
    } 
} 
Các vấn đề liên quan