iOS区分点击了哪个标注配件

标签 ios annotations uibutton mkmapview callout

在我的 map 注释中,我有一个 UIButton 作为标注中的每个辅助 View 。在 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 方法中,我如何找出哪个附件 View 被触摸以处理每个事件?这是我的代码:

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

}

最佳答案

与其设置和使用标签,不如检查 control 是左 View 还是右 View :

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

关于iOS区分点击了哪个标注配件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13691783/

相关文章:

ios - 直接从 UIButton 操作添加 subview ,而无需调用方法

iphone - 仅在存档时使用 iPhone Target 进行部署

iphone - Core Text - 小框文本太多 - 如何添加三个点?

ios - Swift 3 在单元格按钮上添加选择器问题

ios - 5 秒后关闭 UIAlertView Swift

java - Hibernate:映射社交网络中的用户- friend 关系

iphone - 如何在 iPhone 应用程序的单个 map View 中显示多个位置

ios - 如何禁用在 iOS objective-c 中以编程方式创建的 UIButton

iphone - 将 UIView 添加到 UIButton subview 可防止触摸事件

java - Lombok项目是否可以返回对象的接口(interface)?