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

标签 iphone google-maps annotations observer-pattern key-value-observing

我的 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/

相关文章:

iphone - 使用 NSDirectoryEnumerator 迭代目录时从目录中删除文件

javascript - 使用ajax返回谷歌地图标记始终为空

c++ - 从代码中删除 SAL 注释是否安全?

google-maps - 是否可以将 map 中可拖动标记的纬度和经度(使用 javascript)获取到 HTML 表单中?

java - Struts2 注释的还是基于 XML 的?哪个更容易管理和简单?

java - 如何创建注解的实例

iphone - 如何实现天气应用的温度徽章?

iphone - 当有人拥有越狱设备时,objective-c 中的序列化对象的安全性如何?

ios - 通用 ios 应用 iPad 和 iPhone 架构

java - Android - 如何找到两点之间的距离 - Java代码?