2011-03-24 50 views
3

我在iOS的mapView中顯示我的MKPinAnnotationView時出現問題。我得到這個錯誤,但我不明白錯誤來自哪裏:「EXC_BAD_ACCESS」。我的代碼似乎不錯:EXC_BAD_ACCESS with MKPinAnnotationView

-(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) 

的錯誤是在代碼的第二行。任何建議?

感謝您的幫助

+0

你可以複製/粘貼從控制檯的堆棧跟蹤? – Greg 2011-03-24 15:38:36

+0

使用** Build和Debug **運行您的應用程序。發生崩潰時,打開調試器控制檯(Cmd-Shift-R)並鍵入bt。從調試器控制檯複製回溯(在鍵入'bt'後的所有內容)。在此處編輯您的消息並粘貼回溯。 – 2011-03-24 15:42:14

回答

5

您必須初始化使用initWithAnnotation:reuseIdentifier:註解視圖。例如:

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

但是,你也應該利用註解視圖的再利用,通過調用dequeueReusableAnnotationViewWithIdentifier第一。

編輯:
the docs for 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; 
} 
+0

感謝您的幫助。你用dequeueReusableAnnotationViewWithIdentifier表示什麼意思? – Dimitri 2011-03-24 15:53:26

+0

註解視圖像表格視圖單元格,viewForAnnotation就像cellForRowAtIndexPath。爲了獲得更好的性能,可以重新使用註釋視圖。我會在答案中添加詳細信息。 – Anna 2011-03-24 15:57:00

1

嘗試:

MKPinAnnotationView *pv = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];