6

好的,所以你通常有一些你想在 MKMapView 中注释的对象 X。你这样做:

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

然后你在里面创建注释视图

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

当一些标注被点击时,你处理里面的事件:

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

将 X 传递给最新的点击事件的最干净的解决方案是什么?

4

2 回答 2

17

如果我理解您的问题,您应该向您的 DDAnnotation 类添加一个引用或属性,以便在您的 calloutAccessoryControlTapped 方法中您可以访问该对象。

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

创建注释时:

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

然后:

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

    DDAnnotation *anno = view.annotation;
    //access object via
    [anno.objectX callSomeMethod];
}
于 2010-02-26T04:30:34.420 回答
0

我这样做了,效果很好!

这正是我所需要的,因为我需要在点击地图时做一些事情,但让点击进入注释正常流动。

- (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
    }
 }
于 2012-10-23T15:14:02.640 回答