MKMapView iPhone annotation watchers can be selected once

I have different custom map annotations on my MKMapView, and when I create a custom view, I add an observer and turn off 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 a popover and show 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 fired, if I want to select the same annotation, it just doesn’t work ... For example, if the observer state is still “activated”. Therefore, to select my annotation, I need to select another one, and then I can select it again ... This is annoying so that it is not possible to select the same annotation twice in a row.

Please help me! Thank.

+3
3

[mapview deselectAnnotation:annotation animated:FALSE]; , .

+2

: -

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

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

, .

+1

, watchValueForKeyPath , oldObject.

, , , popOverView.

[mapView deselectAnnotation: [ annObject] : NO];

+1

Source: https://habr.com/ru/post/1765336/