开发者

Can't seem to remove any annotations

I have some pins that the user can add. In the callout there is a button. When the user presses the button, I want a new pin to drop in that location, and I want to get rid of the pin that the user pressed (different types of pins).

Basically the first pin is draggable, and when the user has found a proper location they will "lock" it. (the locked pin acts differently in a few ways, which is why I need to replace the old pin)

anyways, this is the method where I do my processing. When I get to the [mapView removeAnnotation:view.annotation]; part all I get is "Program received signal: “EXC_BAD_ACCESS”."

Can anybody help me out here? (The issue is not that the new annotation does not appear, as it does appear. The issue is that the old annotation does not dissap开发者_开发问答ear). EDIT: Fixed code as per suggestion.

    - (void) mapView:(MKMapView *)MapView   
      annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control {
    LockedPotholeAnnotation *annotation = [[[LockedPotholeAnnotation alloc] initWithCoordinate:view.annotation.coordinate addressDictionary:nil]autorelease];
     NSString *titleString = [NSString stringWithFormat:@"Pothole at %.4f, %.4f", view.annotation.coordinate.latitude, view.annotation.coordinate.longitude];
     annotation.title = titleString;
     [mapView addAnnotation:annotation];
     //[annotation release];
     NSLog(@"Added Pin");
     NSLog(@"VA: %@", [view.annotation class]);
     [mapView removeAnnotation:view.annotation];
     //[mapView removeAnnotations:mapView.annotations];
     [mapView setNeedsDisplay]; 
}


This may not be the only thing, but the first thing that leaps out is that you autorelease the annotation on the line where you alloc it. Therefore you shouldn't also release it after you've added it to mapView.

As this stands, the annotation will likely be prematurely deallocated when the autorelease pool drains -- and if not exactly then, then at some subsequent point that is still premature. The map view will be stuck with a stale pointer and boom.

Not sure why that would manifest quite as soon as you describe, so there may be something else too...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜