DetailDisclosure button not showing in annotation view

旧城冷巷雨未停 提交于 2019-12-24 10:58:08

问题


For some odd reason the detail button somehow stopped appearing:

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
if(annotation != mapView.userLocation) 
{
    MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"sadasdasd"];
    if ( pinAnnotation == nil ){
        pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"sadasdasd"] autorelease];

        /* add detail button */
        NSLog(@"Here");

        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinAnnotation.rightCalloutAccessoryView = infoButton;


    }

}

return pinAnnotation;
}

Here is output. Thanks in advance.


回答1:


First problem is that pinAnnotation is declared twice in that method.

Once in the first line and second in the if(annotation != mapView.userLocation)... block. Because of this, the return statement returns nil because the outer variable is never set (resulting in a default MKAnnotationView callout with no accessory).

Change the second declaration to just an assignment.

Next problem is that you need to set canShowCallout to YES because the default is NO for an MKPinAnnotationView. You can do this after setting the accessory view:

pinAnnotation.canShowCallout = YES;

The above should fix the accessory button not showing.


Unrelated, but you also need to set the view's annotation property when it is being re-used (in the case when it is not nil after the dequeue). So add an else block to the if (pinAnnotation == nil):

else {
    //annotation view being re-used, set annotation to current...
    pinAnnotation.annotation = annotation;
}


来源:https://stackoverflow.com/questions/7665068/detaildisclosure-button-not-showing-in-annotation-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!