iOS - Set Detail Disclosure Button on MKAnnotationView
Asked Answered
B

1

5

I have the following method in my MapViewController:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapVC"];
    if (!annotationView) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapVC"];
        annotationView.canShowCallout = YES;
        annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        // could put a rightCalloutAccessoryView here
    } else {
        annotationView.annotation = annotation;
        [(UIImageView *)annotationView.leftCalloutAccessoryView setImage:nil];
    }

    return annotationView;
} 

I believe it's properly set up, but when my map shows my annotations with title and subtitle properly, but they don't show the detail disclosure button, am I missing something?

Another thing is that when debugging this method is never called, yet the annotation view shows up with title and subtitle.

Bhang answered 21/2, 2012 at 4:41 Comment(0)
S
8

Most likely the map view's delegate is not set.

If the delegate is not set or if you don't implement the viewForAnnotation method, the map view will create a default annotation view which is a red pin with a callout containing only the title and subtitle (unless the title is blank in which case you will get a pin but no callout).

Either connect the map view's delegate outlet to File's Owner or in code add this (eg. in viewDidLoad before the annotation is added):

mapView.delegate = self;

Also, if you're not using ARC, add autorelease to the alloc lines to avoid a memory leak.

Sumner answered 21/2, 2012 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.