views:

106

answers:

0

I'm adding an observer to my annotationviews with the following code

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    for (MKAnnotationView *anAnnotationView in views) {
        //[anAnnotationView setCanShowCallout:YES];
        [anAnnotationView addObserver:self
                           forKeyPath:@"selected"
                              options:NSKeyValueObservingOptionNew
                              context:GMAP_ANNOTATION_SELECTED];
    }
}

I then have the following code which will show the UIPopoverController..

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


    NSString *action = (NSString*)context;

    if([action isEqualToString:GMAP_ANNOTATION_SELECTED]){

        BOOL annotationSelected = [[change valueForKey:@"new"] boolValue];
        VehicleInfoViewController *popupView = [[VehicleInfoViewController alloc] init];
        aPopOver = [[UIPopoverController alloc] initWithContentViewController:popupView];

        if (annotationSelected) {
            NSLog(@"Annotation was selected!");

            aPopOver.popoverContentSize = CGSizeMake(250, 300);

            [aPopOver presentPopoverFromRect: CGRectMake(0, 0, 133, 40)
                                        inView:self.view
                                        permittedArrowDirections:UIPopoverArrowDirectionDown
                                        animated:YES];
        } else {
            NSLog(@"Annotation was deselected!");
            //if (![aPopover isPopoverVisible]) {
            //  [aPopover dismissPopoverAnimated:YES];
            //}
        }

    }
}

So, if I click the annotation, I see the "annotation was selected" then if I click the map, I see "annotation was deselected". this is ok... it's when I add the code above (so the aPopOver is shown) when I have trouble

So if the aPopOver is shown and I click the annotation, I do see the message, but if I then click the map (so not selecting the annotation) I do not see the deselected message. Only when I click the map AGAIN do I see it. The first click simply dismisses the uipopovercontroller.

How can I dismiss the popup AND ensure the deselected has been called?