【问题标题】:iPhone MKMapView annotations observers selectable onceiPhone MKMapView 注释观察者可选择一次
【发布时间】: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


【解决方案1】:

我用过[mapview deselectAnnotation:annotation animated:FALSE]; 我认为它到目前为止工作。

【讨论】:

    【解决方案2】:

    尝试改变:-

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

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

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

    【讨论】:

    • 您的解决方案会出现警告。我所做的是在选择注释后取消选择注释:[mapView deselectAnnotation:incidentAnnotation animated:NO];
    【解决方案3】:

    将传递给函数 observeValueForKeyPath 的对象名称保存为临时对象,例如 oldObject

    然后在要关闭 popOverView 的函数中编写下面给出的代码。

    [mapView deselectAnnotation:[oldObject annotation] animated:NO];

    【讨论】:

      猜你喜欢
      • 2014-07-08
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 2013-05-12
      相关资源
      最近更新 更多