开发者

iPhone MKMapView annotations observers selectable once

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
                开发者_StackOverflow社区        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.


I have used [mapview deselectAnnotation:annotation animated:FALSE]; I think it work so far.


Try changing:-

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

to

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

I seem to recall having this problem myself.


Save the object name passing to the function observeValueForKeyPath as a temporary Object say oldObject.

Then write the code given below in function where you are dismissing the popOverView.

[mapView deselectAnnotation:[oldObject annotation] animated:NO];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜