我的 MKMapView 上有不同的自定义 map 注释,在创建自定义 View 时,我添加了一个观察者并禁用默认弹出窗口。
在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;
}
然后在观察者函数中,我创建弹出窗口并显示它:
- (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...");
}
}
我的问题是当我的弹出窗口被关闭时,如果我想选择相同的注释,它就不起作用......就像观察者的状态仍然是“激活”一样。因此,为了选择我的注释,我需要选择另一个注释,然后我可以再次选择它...无法连续两次选择相同的注释,这很烦人。
请帮助我! 谢谢。
最佳答案
我使用了[mapview deselectAnnotation:annotationanimated:FALSE];
我认为到目前为止它有效。
关于iPhone MKMapView注释观察者可选择一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3737645/