iOS 区分点击了哪个标注附件 [英] iOS distinguish between which callout accessory is tapped

查看:21
本文介绍了iOS 区分点击了哪个标注附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的地图注释中,我有一个 UIButton 作为标注中的每个附件视图.在 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 方法中,我如何确定触摸了哪个附件视图来处理每个事件?这是我的代码:

In my map annotations, I have a UIButton as each accessory view in the callouts. In the - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control method, how do I figure out which accessory view was touched to handle each the events? Here's my code:

-(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 是左附件视图还是右附件视图:

Rather than setting and using tags, you could just check if control is the left or right accessory view:

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

这篇关于iOS 区分点击了哪个标注附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆