2

I have a MKMapView with annotations to allow the user to select their desired location. To do this they touch the pin, this displays the name and address in the callout with a leftCalloutAccessoryView that they can touch to select that location.

If they touch the Edit button on the navigationItem I want to remove the leftCalloutAccessoryView and add a rightCalloutAccessoryView. Now, instead of selecting the location I want to open an editor for the user to modify the information.

The code to do this is:

- (void) setEditing: (BOOL) editing animated: (BOOL) animate {
    NSLog( @"Entering %s", __func__ );
    [super setEditing: editing animated: animate];
    // Hide/show the back button consistent with the editing phase...
    // (i.e., hidden while editings, shown when not editing)
    [self.navigationItem setHidesBackButton: editing animated: animate];

    // Modify the mapview annotations
    for( id a in self.mapView.annotations ) {
        MKAnnotationView *av = [self.mapView viewForAnnotation: a];
        av.leftCalloutAccessoryView = editing ? nil : [self chooseLocationAccessoryButton];
        av.rightCalloutAccessoryView = editing ? [UIButton buttonWithType: UIButtonTypeDetailDisclosure] : nil;
    }
}

- (void) mapView: (MKMapView *) mapView annotationView: (MKAnnotationView *) view calloutAccessoryControlTapped: (UIControl *) control {
    NSLog( @"Entering %s", __func__ );
    if( control == view.leftCalloutAccessoryView ) {
        NSLog( @"-- Left button pressed" );
    } else if( control == view.rightCalloutAccessoryView ) {
        NSLog( @"-- Right button pressed" );
    } else {
        NSLog( @"-- Neither right not left button pressed?" );
    }

    if( self.editing ) {
        NSLog( @"-- Should be pushing to edit this location..." );
    } else {
        [self.delegate locationPicker: self didSelectLocation: selectedLocation];
    }
}

The mapView:annotationView:calloutAccessoryControlTapped: method gets called properly unless the callout is visible when I change edit mode. The accessory views nicely animate themselves in and out, but they don't work unless I touch someplace else so the callout disappears, then touch the pin again to show it.

If I change edit mode, then touch a pin, the callout is displayed with the correct accessory view and it is functional.

I feel I must be leaving something out. Can anyone help with keeping my buttons functioning?

LavaSlider
  • 2,494
  • 18
  • 29
  • It's just a hunch but I suspect that this is due to the way that `MKMapView` wires up the accessory views internally, which would be at the time the callout is created. Thus, by the time you tap 'Edit', all that code has already been called and the new views, while animating into place, aren't wired up to the appropriate bits of code that ultimately call `calloutAccessoryControlTapped`. Your best bet might be to manually close any open callouts when Edit is tapped, then programatically re-select the annotation. – Carlos P Sep 21 '13 at 22:55

0 Answers0