3

我對我的MKMapView有不同的自定義地圖註釋,當創建自定義視圖時,我添加一個觀察者並禁用默認彈出窗口。iPhone 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..."); 
    } 
} 

我的問題是,我想選擇相同的註釋,它只是不工作...就好像觀察者的狀態仍然「激活」。所以爲了選擇我的註釋,我需要選擇另一個註釋,然後我可以再次選擇它......這很煩人,因爲無法連續兩次選擇相同的註釋。

請幫幫我! 謝謝。

+0

您的代碼格式已損壞 - 請重新格式化以使其更具可讀性。 – Echelon 2010-09-17 18:42:58

+0

我沒有重新格式化代碼,對不起。 – Dachmt 2010-09-20 19:47:36

回答

1

嘗試改變: -

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

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

我似乎記得有這個問題我自己。

+0

解決方案出現警告。我做的是取消選擇註釋後的註釋:[mapView deselectAnnotation:incidentAnnotation animated:NO]; – Dachmt 2010-09-20 19:48:46

2

我已經使用[mapview deselectAnnotation:annotation animated:FALSE]; 我認爲它工作到目前爲止。

1

保存傳遞給函數observeValueForKeyPath的對象名稱作爲臨時對象,如oldObject

然後在下面給出的代碼中寫下你在解除popOverView的函數中的代碼。

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