2010-02-24 7 views
2

Je suis en train d'utiliser la propriété « sélectionnée » dans MKAnnotationView (discuté here), cela est pour l'utilisateur de pouvoir supprimer une annotation sélectionnée ...Accéder à la propriété "selected" de MKAnnotationView personnalisé?

Le morceau de code suivant doit trouver la broche sélectionnée dans MKMapView et retirez-le:

CSMapAnnotation *a; 

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

Où CSMapAnnotation est ma carte personnalisée Annotation définie comme ceci:

#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; 

Je pense que parce que je ne suis pas vraiment « héritant » de MKAnnotationView, CSMapAnnotation ne répondra pas à selected.

Quelle serait la meilleure façon de résoudre ce problème?

Répondre

1

Vous avez raison dans votre hypothèse; Comme CSMapAnnotation n'hérite pas de MKAnnotationView et que vous n'avez pas implémenté la propriété sélectionnée, cela ne fonctionnera pas.

En outre, gérez-vous la relation CSMapAnnotation à MKAnnotationView pour mapper la vue d'annotation (la broche) aux données stockées dans CSMapAnnotation? Rappelez-vous que MKAnnotationView a la propriété sélectionnée, pas MKAnnotation.

Si vous gérez l'annotation pour voir la cartographie correctement, cela devrait fonctionner pour vous:

CSMapAnnotation *a; 

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

Ou encore:

[mapView removeAnnotations:[mapView selectedAnnotations]];