我有一个带有标注的 MKMapView。按下标注(详细信息披露指示器)按钮时,它会显示一个 detailViewController。我想为我的注释添加更改地图类型和图钉颜色的功能。所以我目前这样做:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
DetailMapViewController *destination = [[DetailMapViewController alloc] initWithNibName:@"DetailMapViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:destination];
if ([destination respondsToSelector:@selector(setDelegate:)]) {
[destination setValue:self forKey:@"delegate"];
}
if ([destination respondsToSelector:@selector(setPlacemark:)]) {
[destination setValue:_currentPlacemark forKey:@"Placemark"];
}
if ([destination respondsToSelector:@selector(setMapTypeAsNum:)]) {
[destination setValue:[NSNumber numberWithInteger:self.mapView.mapType] forKey:@"mapTypeAsNum"];
[destination addObserver:self forKeyPath:@"mapTypeAsNum" options:NSKeyValueObservingOptionNew context:NULL];
}
if ([destination respondsToSelector:@selector(setColorAsNum:)]) {
[destination setValue:[NSNumber numberWithInteger:MKPinAnnotationColorPurple] forKey:@"colorAsNum"];
[destination addObserver:self forKeyPath:@"colorAsNum" options:NSKeyValueObservingOptionNew context:NULL];
}
[self presentModalViewController:navController animated:YES];
}
在我的 observeValueForKeyPath 方法中:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
DetailMapViewController *destination = (DetailMapViewController *)object;
[destination removeObserver:self forKeyPath:@"mapTypeAsNum"];
[destination removeObserver:self forKeyPath:@"colorAsNum"];
if ([keyPath isEqualToString:@"mapTypeAsNum"]) {
if ([object isKindOfClass:[DetailMapViewController class]]) {
id mapType = [change objectForKey:NSKeyValueChangeNewKey];
self.mapView.mapType = [mapType integerValue];
}
}
else if ([keyPath isEqualToString:@"colorAsNum"]) {
if ([object isKindOfClass:[DetailMapViewController class]]) {
id pinColor = [change objectForKey:NSKeyValueChangeNewKey];
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView viewWithTag:10];
annotationView.pinColor = [pinColor integerValue];
}
}
}
这样做的问题是,如果我不更改 DetailViewController 中的引脚颜色或地图类型来调用 observeValueForKeyPath 方法,我最终会根据控制台泄漏观察信息。所以我想知道在这样的场景中我在哪里添加观察者和删除观察者。在过去使用 NSNotificationCenter 时,我会在 dealloc 中删除观察者,并在呈现与上述代码类似的 detailViewController 之前添加观察者。但是对于 KVO,我不确定它是如何工作的。有什么想法吗?谢谢。