Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rightCalloutAccessoryView not shown on the MKPinAnnotationView

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.

like image 783
Jason Lee Avatar asked Mar 24 '23 20:03

Jason Lee


1 Answers

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:

  1. Your viewForAnnotation is returning nil in all cases. Make sure you you return annotationView after creating/modifying it.

  2. 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):

    enter image description here

    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.

like image 166
Rob Avatar answered Apr 19 '23 23:04

Rob