iOS различает, какой аксессуар выноски нажат

В моих аннотациях к карте у меня есть UIButton в качестве каждого вспомогательного представления в выносках. В методе - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control как мне выяснить, какое вспомогательное представление было затронуто для обработки каждого события? Вот мой код:

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

if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];

UIButton *calloutButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
UIButton *directionsButton = [UIButton buttonWithType:UIButtonTypeCustom];
directionsButton.frame = CGRectMake(0, 0, 23, 23);
[directionsButton setBackgroundImage:[UIImage imageNamed:@"directions.png"] forState:UIControlStateNormal];

MyPin.leftCalloutAccessoryView = directionsButton;
MyPin.rightCalloutAccessoryView = calloutButton;
MyPin.draggable = NO;
MyPin.highlighted = NO;
MyPin.animatesDrop= YES;
MyPin.canShowCallout = YES;
MyPin.pinColor = MKPinAnnotationColorRed;

return MyPin;
}

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

Annotation *ann = view.annotation;

if ([control tag] == 1) {

    CLLocationCoordinate2D currentCoords = {ann.coordinate.latitude, ann.coordinate.longitude};

    MKPlacemark *place = [[MKPlacemark alloc] initWithCoordinate: currentCoords addressDictionary:nil];
    MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark: place];
    destination.name = ann.title;
    destination.url = [NSURL URLWithString:@"http://www.wccca.com/PITS"];
    NSArray *items = [[NSArray alloc] initWithObjects: destination, nil];
    NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                             MKLaunchOptionsDirectionsModeDriving,
                             MKLaunchOptionsDirectionsModeKey, nil];
    [MKMapItem openMapsWithItems: items launchOptions: options];

}

if ([control tag] == 2) {

    MKCoordinateRegion region;
    region.center.latitude = ann.coordinate.latitude;
    region.center.longitude = ann.coordinate.longitude;
    region.span.latitudeDelta  = 0.02;
    region.span.longitudeDelta = 0.02;

    [self.mapView setRegion:region animated:YES];
}

}

person Jon Erickson    schedule 03.12.2012    source источник
comment
в вашем коде вы уже используете теги .. разве этого недостаточно, чтобы узнать, какой uibutton был нажат?   -  person poncha    schedule 04.12.2012
comment
он не работает по какой-то причине. Я не знаю почему!   -  person Jon Erickson    schedule 04.12.2012


Ответы (1)


Вместо того, чтобы устанавливать и использовать теги, вы можете просто проверить, является ли control левым или правым вспомогательным видом:

if (control == view.leftCalloutAccessoryView) {
    //handle left control tap...        
}
else
if (control == view.rightCalloutAccessoryView) {
    //handle right control tap...       
}
person Community    schedule 03.12.2012