【发布时间】:2010-09-17 17:43:12
【问题描述】:
我的 MKMapView 上有不同的自定义地图注释,在创建自定义视图时,我添加了一个观察者并禁用了默认弹出窗口。
在 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...");
}
}
我的问题是当我的弹出框被关闭时,如果我想选择相同的注释,它就不起作用......就像观察者的状态仍然“激活”一样。所以为了选择我的注解,我需要选择另一个,然后我可以再次选择它...... 不能连续选择两次相同的注解很烦人。
请帮帮我! 谢谢。
【问题讨论】:
-
您的代码格式已损坏 - 请重新格式化以使其更具可读性。
-
我确实重新格式化了代码,抱歉。
标签: iphone google-maps annotations observer-pattern key-value-observing