清洁解决方案,以了解哪些MKAnnotation已被挖掘?

Ste*_*rna 6 cocoa-touch mkmapview mkannotation

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

DDAnnotation *annotation = [[DDAnnotation alloc] initWithCoordinate: poi.geoLocation.coordinate title: @"My Annotation"];
[_mapView addAnnotation: annotation];
Run Code Online (Sandbox Code Playgroud)

然后在里面创建注释视图

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
Run Code Online (Sandbox Code Playgroud)

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

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
Run Code Online (Sandbox Code Playgroud)

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

小智 17

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

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

创建注释时:

DDAnnotation *annotation = [[DDAnnotation alloc] initWithCoordinate:poi.geoLocation.coordinate title: @"My Annotation"];
annotation.objectX = objectX;
[_mapView addAnnotation: annotation];
Run Code Online (Sandbox Code Playgroud)

然后:

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

    DDAnnotation *anno = view.annotation;
    //access object via
    [anno.objectX callSomeMethod];
}
Run Code Online (Sandbox Code Playgroud)