Remove KVO for MKAnnotationViews
Remove KVO for MKAnnotationViews
- Subject: Remove KVO for MKAnnotationViews
- From: Philip Vallone <email@hidden>
- Date: Sun, 10 Apr 2011 07:46:40 -0400
Hi,
I am in need of some help. I have a MKMapView object, which allows the user to add, move and delete MKAnnotationViews. When an Annotation is added to the map, I use KVO to track if the Annotation was moved or selected:
for (MKAnnotationView *anAnnotationView in views) {
[anAnnotationView addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:@"MapAnnontationSelected"];
}
Here is how I add the annotation:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.animatesDrop= YES;
annView.canShowCallout = YES;
annView.draggable = YES;
UIImage *image = [UIImage imageNamed:@"gps_add.png"];
UIImageView *imgView = [[[UIImageView alloc] initWithImage:image] autorelease];
annView.leftCalloutAccessoryView = imgView;
annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.pinColor = MKPinAnnotationColorGreen;
return [annView autorelease];
}
The problem occurs when I delete the Annotation. Since the MKPinAnnotationView is set to auto release, the object is deallocated with the observer still attached.
"An instance 0xb323890 of class MKPinAnnotationView was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:"
I tried to remove the observer when the pin gets deleted, but that still gives the error:
for (id <MKAnnotation> annotation in mapView.annotations) {
[[mapView viewForAnnotation:annotation] removeObserver:self forKeyPath:@"selected"];
}
Any help would be greatly appreciated.
Thx.
Phil
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden