Right Callout Accessory method and implementation Right Callout Accessory method and implementation xcode xcode

Right Callout Accessory method and implementation


The method name is wrong. It should be mapView with a capital V:

- (MKAnnotationView *)mapView:(MKMapView *)sender             viewForAnnotation:(id <MKAnnotation>)annotation 

Objective-C is case-sensitive.

If the method still doesn't get called, then the other problem is that the map view's delegate is not set. In code, set it to self or in Interface Builder attach the delegate to File's Owner.

Also make sure you set the title of the annotation before adding it otherwise the callout still won't show.

The above changes should fix the accessory button not appearing.


Some other unrelated suggestions...

In viewForAnnotation, you should support annotation view re-use by calling dequeueReusableAnnotationViewWithIdentifier:

- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id < MKAnnotation >)annotation{    static NSString *reuseId = @"StandardPin";    MKPinAnnotationView *aView = (MKPinAnnotationView *)[sender                 dequeueReusableAnnotationViewWithIdentifier:reuseId];    if (aView == nil)    {        aView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation                     reuseIdentifier:reuseId] autorelease];        aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];        aView.canShowCallout = YES;    }    aView.annotation = annotation;    return aView;   }

If your project uses ARC, remove the autorelease.


By the way, to respond to the accessory button press, implement the calloutAccessoryControlTapped delegate method:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view         calloutAccessoryControlTapped:(UIControl *)control{    NSLog(@"accessory button tapped for annotation %@", view.annotation);}