cocoa-touch - 知道要使用哪个MKAnnotation的干净解决方案?

标签 cocoa-touch mkmapview mkannotation

好的,因此通常在MKMapView中有一些要注释的对象X。您可以这样进行:

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

然后在里面创建注释 View
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;

当一些标注被点击时,您可以在内部处理事件:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;

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

最佳答案

如果我理解您的问题,则应将引用或属性添加到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];
}

关于cocoa-touch - 知道要使用哪个MKAnnotation的干净解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2329654/

相关文章:

iphone - 以随机时间间隔执行方法

ios - UIScrollView 覆盖了我的 subview 的平移手势识别器

objective-c - 保存和恢复自定义对象数组

ios - 拖动我的 MKAnnotationView 后拖动 map 不会随之移动 MKAnnotationView

ios - 向 MKMapView 注释添加操作

ios - 代码 8 : basic MKMapView causes warnings

MKMapView 的 iOS 自定义注释

iphone - 如何判断一个 View 是否是第一次加载?

ios - MKAnnotationView 正在失去对 MKAnnotation 的引用

ios - mapView.annotations.count 在 swift 中不为 0