2012-12-03 11 views
5

In meinen Kartenanmerkungen habe ich eine UIButton als jede Zubehöransicht in den Callouts. In der - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control Methode, wie finde ich heraus, welche Zubehöransicht berührt wurde, um die Ereignisse zu behandeln? Hier ist mein Code ist:iOS unterscheidet, welches Callout-Zubehör angezapft wird

-(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]; 
} 

} 
+0

in Ihrem Code sind Sie bereits Tags verwenden .. ist nicht das genug, um zu wissen, welches Uibutton angezapft wurde? – poncha

+0

es funktioniert aus irgendeinem Grund nicht. Ich weiß nicht warum! –

Antwort

18

Anstatt Einstellung und Verwendung von Tags, Sie könnten nur überprüfen, ob control die linke oder rechte Zubehör Ansicht ist:

if (control == view.leftCalloutAccessoryView) { 
    //handle left control tap...   
} 
else 
if (control == view.rightCalloutAccessoryView) { 
    //handle right control tap...  
} 
+0

Perfekt. Vielen Dank –