views:

66

answers:

3

Hi,

I have different custom map annotations on my MKMapView, and when creating the custom view I add an observer and disable the default popup.

At the top of MapViewController.m:

static NSString* const ANNOTATION_SELECTED_DESELECTED = @"annotationSelectedOrDeselected";

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    // Things here.

    // Enable the view.
    [annotationView setEnabled:YES];

    // Delete the default popup.
    [annotationView setCanShowCallout:NO];

    // Add an observer on the annotation.
    [annotationView addObserver:self
                     forKeyPath:@"selected"
                        options:NSKeyValueObservingOptionNew
                        context:ANNOTATION_SELECTED_DESELECTED];

    return annotationView;
}

Then in the observer function, I create the popover and display it:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSString *action = (NSString *)context;

    if ([action isEqualToString:ANNOTATION_SELECTED_DESELECTED]) {
        BOOL annotationSelected = [[change valueForKey:@"new"] boolValue];

        if (annotationSelected) {
            // Actions when annotation selected.
            // I create the appropriate popover here and display it in self.view
        }
    } else {
        // Actions when annotation deselected.
        NSLog(@"Annotation deselected! But never pass here...");
    }
}

My problem is when my popover is dismissed, if I want to select the same annotation, it just doesn't work... Like if the state of the observer is still "activated". So in order to select my annotation, I need to select another one, and then I can select it again... It's annoying to not be able to select the same annotation twice in a row.

Please help me! Thank you.

A: 

Try changing:-

BOOL annotationSelected = [[change valueForKey:@"new"] boolValue];

to

BOOL annotationSelected = [[change valueForKey:NSKeyValueObservingOptionNew] boolValue];

I seem to recall having this problem myself.

Echelon
A warning appears with your solution. What I did is deselecting the annotation after the annotation has been selected: [mapView deselectAnnotation:incidentAnnotation animated:NO];
Dachmt
A: 

yes, i try this on simulator 4.1 and he say that it is a mistake, because "NSKeyValueObservingOptionNew" is not a NSString. can you please tell me what i should do?