Search code examples
objective-cuibuttonmkannotationview

UIButton Not Loading Properly For MKAnnotationView


I am creating an MKAnnotationView with a detail disclosure button.

In mapView: viewForAnnotation: I just create an placeholder button.

//  the right accessory view needs to be a disclosure button ready to bring up the photo
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

In mapView: didSelectAnnotationView: I actually create a button to be used (with the relevant tag)

//  create a button for the callout
UIButton *disclosure                = [self.delegate mapController:self buttonForAnnotation:aView.annotation];

NSLog(@"DisclosureButton: %@", disclosure);

//  set the button's target for when it is tapped upon
[disclosure addTarget:self.delegate action:@selector(presentAnnotationPhoto:) forControlEvents:UIControlEventTouchUpInside];

//  make the button the right callout accessory view
aView.rightCalloutAccessoryView = disclosure;

In the log, the button appears to be fully instantiated as well as set with the correct tag.

This is the button creator:

/**
 *  returns an button for a specific annotation
 *
 *  @param  sender              the map controller which is sending this method to us (its' delegate)
 *  @param  annotation          the annotation we need to create a button for
 */
- (UIButton *)mapController:(MapController *)   sender
        buttonForAnnotation:(id <MKAnnotation>) annotation
{
    //  get the annotation as a flickr photo annotation
    FlickrPhotoAnnotation *fpa  = (FlickrPhotoAnnotation *)annotation;

    //  create a disclosure button used for showing photo in callout
    UIButton *disclosureButton      = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    //  associate the correct photo with the button
    disclosureButton.tag            = [self.photoList indexOfObject:fpa.photo];

    return disclosureButton;
}

The problem comes when I select the annotation. For a few seconds when the annotation is selected and the detail disclosure button is tapped, nothing happens. However, after tapping away and back onto the annotation a few times and testing the button, it eventually works as expected.

What is going on with the strange delay? Sometimes when the button is going to work, it just appears as if the alpha is set to 0.0 until you tap on it and it appears.

Seriously one of the more odd problems I've encountered.


Solution

  • Before the didSelectAnnotationView delegate method is called, the map view has already prepared the callout view based on the annotation view's properties (before your changes).

    So the callout you see the on the first tap is without the changes the app makes in didSelectAnnotationView. On the following taps, the callout could be based on the values set from the previous tap (this actually depends on how annotation view re-use is handled in viewForAnnotation).

    It looks like the only things the code is doing in didSelectAnnotationView and buttonForAnnotation is setting the button action and tag.

    I assume you're using the "tag" approach because the presentAnnotationPhoto: method needs to reference the selected annotation's properties.

    You don't need to use a tag to get the selected annotation in your action method. Instead, there are a couple of better options:

    • Your custom action method can get the selected annotation from the map view's selectedAnnotations property. See this question for an example of how to do this.
    • Use the map view's own delegate method calloutAccessoryControlTapped instead of a custom action method. The delegate method passes a reference to the annotation view which contains a property pointing to its annotation (ie. view.annotation) so there's no guessing, searching, or question as to what annotation was selected. I recommend this option.

    In the first option, do the addTarget in viewForAnnotation and don't bother setting the tag. You also don't need the buttonForAnnotation method. Then in the button action method, get the selected annotation from mapView.selectedAnnotations.

    Currently, your action method is on self.delegate so you might have some trouble accessing the map view from that other controller. What you can do is create a local button action method in the map controller which gets the selected annotation and then calls the presentAnnotationPhoto: action method on self.delegate (except now that method can be written to accept an annotation parameter instead of being a button tap handler).

    The second option is similar except you don't need to do any addTarget and in the calloutAccessoryControlTapped method, call presentAnnotationPhoto: on self.delegate.

    For both options, I suggest modifying the presentAnnotationPhoto: method to accept the annotation object itself (FlickrPhotoAnnotation *) instead of the current UIButton * and in the map controller, do an addTarget on a method local to the map controller (or use calloutAccessoryControlTapped) and from that method, manually call presentAnnotationPhoto: and pass it the annotation.