iPhone MKMapView注释观察者可选择一次
我的 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...");
}
}
我的问题是当我的弹出窗口被关闭时,如果我想选择相同的注释,它就不起作用......就像如果观察者的状态仍然是“激活”。因此,为了选择我的注释,我需要选择另一个注释,然后我可以再次选择它...无法连续两次选择相同的注释,这很烦人。
请帮我! 谢谢。
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用了
[mapview deselectAnnotation:annotationAnimated:FALSE];
我认为到目前为止它有效。
I have used
[mapview deselectAnnotation:annotation animated:FALSE];
I think it work so far.
尝试改变:-
我
似乎记得自己也遇到过这个问题。
Try changing:-
to
I seem to recall having this problem myself.
将传递给函数observeValueForKeyPath 的对象名称保存为临时对象,例如oldObject。
然后在要关闭 popOverView 的函数中编写下面给出的代码。
[mapView deselectAnnotation:[oldObject 注释] 动画:NO];
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];