Matt
Matt

Reputation: 435

mkmapview delegate method not being called -calloutaccessorycontroltapped

I'm trying to complete the last part of assignment 5 of cs193p (link)

The app selects popular places from Flickr and plots photos from those places on the map.

It's Task 5c which says

Each annotation on the map should have a callout which displays the following: c. A disclosure button which brings up the full image of the chosen photo (exactly as if the user had chosen the photo from a table) or which brings up a list of photos in that place (again, exactly as if the user had chosen that place from a table).

I've already completed the previous parts which display the callout, and load a thumbnail into the callout.

I've already set up my mapViewController as the delegate of the mapView, and I know it's working because when I click on the pin, it calls the didSelectAnnotationView method, which is also in my mapViewController and that all runs fine, and actually displays the disclosure accessory in the callout.

However, I've also defined the method calloutAccessoryControlTapped (same VC) and this method does not get called. At all. At the moment I've just got an NSLog in there, and nothing comes up.

Well I say nothing, just to make it more weird, there is one annotation that it works for out of hundreds that don't.

One annotation from the very first place fires the button, but nothing else does. Is it because it's the first annotation? Is the button only firing for that?

I wanted to post code, but not really sure how relevant it is:

 @interface flickrthingMapViewController ()<MKMapViewDelegate>
    @property (weak, nonatomic) IBOutlet MKMapView *mapview;

    @end

    @implementation flickrthingMapViewController
    @synthesize delegate = _delegate;

    @synthesize mapview = _mapview;
    @synthesize annotations = _annotations;


    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.mapview.delegate = self; //remembered to set the delegate

    }
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView
{
    if (![aView.annotation isKindOfClass:[flickrthingPlaceAnnotation class]])
    {
        UIImage *image = [self.delegate flickrthingMapViewContoller:self imageForAnnotation:aView.annotation];


        [(UIImageView *)aView.leftCalloutAccessoryView setImage:image];
    }
    UIButton *buttonToViewDetails = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    aView.rightCalloutAccessoryView=buttonToViewDetails;


}

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)aView calloutAccessoryControlTapped:(UIControl *)control
{

    NSLog(@"hello");
}

Every other question I've asked it turns out I've done something silly, and I'm hoping that's the case here. But I've been sat looking at it for ages and can't see it!

Thanks for your insights!

Upvotes: 1

Views: 1719

Answers (1)

Matt
Matt

Reputation: 435

It turns out, that adding the button in didSelectAnnotationView is too late!

I thought I was trying to be efficient by not creating the button unless someone actually selected the annotation, but it turns out that (for some reason?) if the button is created in didSelectAnnotationView it doesn't trigger the delegate method.

I've moved it into the viewForAnnotation method and it works!

Upvotes: 2

Related Questions