问题
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