iPhone MKMapView注释观察者可选择一次

发布于 2024-09-24 01:57:56 字数 1580 浏览 5 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野却迷人 2024-10-01 01:57:56

我使用了 [mapview deselectAnnotation:annotationAnimated:FALSE];
我认为到目前为止它有效。

I have used [mapview deselectAnnotation:annotation animated:FALSE];
I think it work so far.

剩一世无双 2024-10-01 01:57:56

尝试改变:-

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

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

似乎记得自己也遇到过这个问题。

Try changing:-

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

to

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

I seem to recall having this problem myself.

杯别 2024-10-01 01:57:56

将传递给函数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];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文