3

我在 iOS 的 mapView 上显示我的 MKPinAnnotationView 时遇到问题。我收到此错误,但我不明白错误来自哪里:“EXC_BAD_ACCESS”。我的代码看起来不错:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

MKPinAnnotationView *pv = [[MKPinAnnotationView alloc] init];
[pv setPinColor:MKPinAnnotationColorGreen];
[pv setCanShowCallout:YES];
[pv setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];

return pv;

}

当我为应用程序设置断点时,这是我使用 gdb 的控制台显示:

    continue
Current language:  auto; currently objective-c
Program received signal:  “EXC_BAD_ACCESS”.
(gdb) bt
#0  0x023d1dfe in -[MKPinAnnotationView _setPinType:] ()
#1  0x0000324c in -[iSouvenirView mapView:viewForAnnotation:] (self=0x761abe0, _cmd=0x24221fd, mapView=0x7613410, annotation=0x761d360) at /Users/m2sar/iSouvenir/iSouvenirView.m:125
#2  0x02398130 in -[MKAnnotationContainerView _addViewForAnnotation:] ()
#3  0x02392b2a in -[MKAnnotationContainerView _addViewsForAnnotations:animated:] ()
#4  0x0238e657 in -[MKAnnotationContainerView showAddedAnnotationsAnimated:] ()
#5  0x0236837c in -[MKMapView _showAddedAnnotationsAndRouteAnimated:] ()
#6  0x02367cc8 in -[MKMapView mapTileViewDidFinishRendering:] ()
#7  0x023e7ae0 in -[MKMapTileView _didFinishRendering] ()
#8  0x000471c9 in __NSFireTimer ()
#9  0x025cff73 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#10 0x025d15b4 in __CFRunLoopDoTimer ()
#11 0x0252ddd9 in __CFRunLoopRun ()
#12 0x0252d350 in CFRunLoopRunSpecific ()
#13 0x0252d271 in CFRunLoopRunInMode ()
#14 0x0326000c in GSEventRunModal ()
#15 0x032600d1 in GSEventRun ()
#16 0x002bfaf2 in UIApplicationMain ()
#17 0x00002388 in main (argc=1, argv=0xbffff070) at /Users/m2sar/iSouvenir/main.m:14
(gdb)

错误在代码的第二行。有什么建议吗?

谢谢你的帮助

4

2 回答 2

5

您必须使用 初始化注释视图initWithAnnotation:reuseIdentifier:。例如:

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

但是,您还应该通过首先调用来利用注释视图重用dequeueReusableAnnotationViewWithIdentifier

编辑:
MKAnnotationView 的文档中,标题为“重用注释视图”的段落解释了为什么应该使用dequeueReusableAnnotationViewWithIdentifier. 因此 viewForAnnotation 的代码如下所示:

-(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;
}
于 2011-03-24T15:47:39.507 回答
1

尝试:

MKPinAnnotationView *pv = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
于 2011-03-24T15:51:24.093 回答