0

So I have MKAnnotationViews on the map in my app, however, I cannot seem to get the annotation to show a callout. Heres some of the code I've tried:

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

static NSString *identifier = @"Bar";   
if ([annotation isKindOfClass:[BarLocations class]]) {

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = YES;

//Doesn't work
    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    button.frame = CGRectMake(0, 0, 23, 23);
    annotationView.rightCalloutAccessoryView = button;

//Doesn't work
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    [leftButton setTitle:annotation.title forState:UIControlStateNormal];
    [annotationView setLeftCalloutAccessoryView:leftButton];
    annotationView.canShowCallout = YES;

//Doesn't work

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    rightButton.frame = CGRectMake(0, 0, 15, 15);
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    annotationView.rightCalloutAccessoryView = rightButton;


return annotationView; 
}

Note - I tried each of those separately - I just put them together in this post to show options I've already tried.

What am I doing wrong?

Edit

I think I figured out a big part of the problem: The following method is never called:

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

However, I thought MkMapView's are like UITableView's. So how do I call it. Did I forget to assign a delegate?

Any help would be appreciated

Andrew
  • 3,874
  • 5
  • 39
  • 67
  • Are you returning annotationView? – Philip Jul 14 '12 at 19:09
  • Yes - the annotations show up on my map (with the title and subtitle), its just the callouts that do not – Andrew Jul 14 '12 at 19:21
  • Is the delegate method actually called and does the line that sets rightCalloutAccessoryView actually run (step through the method in the debugger)? –  Jul 14 '12 at 19:27
  • I'm comparing code in one of my projects and they're very similar. A few differences: 1. We don't set the frame on the button. We don't dequeue from the mapkit view (not for any particular reason, although the docs explicitly recommend this), and we've also implemented - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control – Philip Jul 14 '12 at 19:28
  • Check out my edit. I think I'm on the cusp of solving it – Andrew Jul 14 '12 at 19:37
  • That was it. I forgot to assign the delegate from the map to the class. I feel really stupid for forgetting this basic step and struggling with this for 2 days.... – Andrew Jul 14 '12 at 19:41

1 Answers1

0

So the problem turns out to be I forgot to link the mapView's delegate to the class where its implemented. So even though the methods were there, and correct, the delegate methods were never called. Once I connected the delegate, it worked perfectly.

Andrew
  • 3,874
  • 5
  • 39
  • 67