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?