I use the MKPinAnnotationView on the MKMapView, the title and subtitle is shown normally, but the rightCalloutAccessoryView
is not shown.
I tried google a lot, but could not show it yet. My code is below.
- (void)addAnnotation
{
CLLocationCoordinate2D theCoordinate = self.location.coordinate;
MapAnnotation *annotation = [[MapAnnotation alloc] initWithCoordinate:theCoordinate];
annotation.title = @"Pin";
annotation.subtitle = @"More information";
[self.mapView addAnnotation:annotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MapAnnotation class]]) {
static NSString *reuseIdentifier = @"MyMKPinAnnotationView";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
if (!annotationView) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
annotationView.pinColor = MKPinAnnotationColorRed;
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
} else {
annotationView.annotation = annotation;
}
}
return nil;
}
Notice that title & subtitle can be shown.
Thanks in advance.
Seeing the title and subtitle is the default behavior of an annotation view. If you want to do anything else (such as the rightCalloutAccessorView
), you have to make sure your viewForAnnotation
is called by setting the delegate
of your MKMapView
and also make sure that your viewForAnnotation
is returning the annotationView
you're creating.
One of two problems is occurring:
Your viewForAnnotation
is returning nil
in all cases. Make sure you you return annotationView
after creating/modifying it.
If your map view's delegate
hasn't been set, your current viewForAnnotation
would not be called at all.
Make sure you have set the delegate
. You can do this programmatically:
self.mapView.delegate = self;
Or you can do this in IB (by selecting the connections inspector, the rightmost tab of the right panel and control-drag from the delegate
to the status bar of the scene):
If this is the case, if you put a NSLog
statement or breakpoint in your viewForAnnotation
. You might not see it being called at all if the delegate
hasn't been set up properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With