访问自定义MKAnnotationView的“selected”属性?

时间:2010-02-24 17:09:49

标签: iphone cocoa-touch mkmapview mkannotation

我正在尝试使用MKAnnotationView中的“selected”属性(讨论here),这是为了让用户能够删除选定的注释......

以下代码应该在MKMapView中找到所选的引脚并将其删除:

CSMapAnnotation *a;

for(a in [mapView annotations])
{
    if([a selected]) //Warning: 'CSMapAnnotation' may not respond to '-selected'
    {
        [mapView removeAnnotation:a];
    }
}

CSMapAnnotation是我的自定义地图注释,其定义如下:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

// types of annotations for which we will provide annotation views. 
typedef enum {
    kCMapAnnotationTypeStart        = 0,
    kCMapAnnotationTypeCheckpoint   = 1,
    kCMapAnnotationTypeEnd          = 2
} CSMapAnnotationType;

@interface CSMapAnnotation : NSObject <MKAnnotation>
{
    CLLocationCoordinate2D coordinate;
    CSMapAnnotationType    annotationType;
    NSString*              title;
    NSString*              userData;
}

-(id) initWithCoordinate:(CLLocationCoordinate2D)inCoordinate 
          annotationType:(CSMapAnnotationType) annotationType
                   title:(NSString*)title;

- (BOOL) isEqualToAnnotation:(CSMapAnnotation *) anAnnotation;

@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
@property (nonatomic, readwrite) CSMapAnnotationType    annotationType;
@property (nonatomic, retain) NSString* title;
@property (nonatomic, retain) NSString* userData;

我认为,因为我并非真正“继承”MKAnnotationViewCSMapAnnotation不会回复selected

解决此问题的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

你的假设是正确的;由于CSMapAnnotation不从MKAnnotationView继承,并且您没有实现所选属性,因此它不起作用。

另外,您是否管理CSMapAnnotation与MKAnnotationView关系以将注释视图(引脚)映射到CSMapAnnotation中存储的数据?请记住,MKAnnotationView具有selected属性,而不是MKAnnotation。

如果您正在管理注释以正确查看映射,这应该适合您:

CSMapAnnotation *a; 

for(a in [mapView selectedAnnotations])
{
    //You may want a type-check here
    [mapView removeAnnotation:a];
}

甚至:

[mapView removeAnnotations:[mapView selectedAnnotations]];