EXC_BAD_ACCESS with MKPinAnnotationView

隐身守侯 提交于 2019-12-01 21:13:39

You must init the annotation view using initWithAnnotation:reuseIdentifier:. For example:

MKPinAnnotationView *pv = [[[MKPinAnnotationView alloc] 
    initWithAnnotation:annotation reuseIdentifier:@"annot"] autorelease];

However, you should also take advantage of annotation view re-use by calling dequeueReusableAnnotationViewWithIdentifier first.

Edit:
In the docs for MKAnnotationView, the paragraph titled "Reusing Annotation Views" explains why you should use dequeueReusableAnnotationViewWithIdentifier. So the code for viewForAnnotation would look like this:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{
    static NSString *myAnnotationIdentifier = @"annot";

    MKPinAnnotationView *pv = (MKPinAnnotationView *)[mapView 
        dequeueReusableAnnotationViewWithIdentifier:myAnnotationIdentifier];
    if (!pv)
    {
        pv = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                reuseIdentifier:myAnnotationIdentifier] autorelease];
        [pv setPinColor:MKPinAnnotationColorGreen];
        [pv setCanShowCallout:YES];
        [pv setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];
    }
    else
    {
        //we're re-using an annotation view
        //update annotation property in case re-used view was for another  
        pv.annotation = annotation;
    }

    return pv;
}

Try:

MKPinAnnotationView *pv = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!