我對我的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...");
}
}
我的問題是,我想選擇相同的註釋,它只是不工作...就好像觀察者的狀態仍然「激活」。所以爲了選擇我的註釋,我需要選擇另一個註釋,然後我可以再次選擇它......這很煩人,因爲無法連續兩次選擇相同的註釋。
請幫幫我! 謝謝。
您的代碼格式已損壞 - 請重新格式化以使其更具可讀性。 – Echelon 2010-09-17 18:42:58
我沒有重新格式化代碼,對不起。 – Dachmt 2010-09-20 19:47:36