如何在annotationView上使用按钮来执行iphone中的操作?

时间:2011-11-22 13:41:38

标签: iphone ios button annotations mkmapview

我想在iphone的mapkit中的annotationView中添加按钮,以便在单击该按钮时执行操作。如何点击该按钮执行操作?我也无法使用以下代码在注释视图中添加按钮:

- (MKAnnotationView *)viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
    static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil )
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorRed; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;

        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [infoButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        pinView.rightCalloutAccessoryView = infoButton;

    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}

任何人都可以告诉我如何在默认用户位置的蓝点注释视图中添加按钮?提前谢谢。

3 个答案:

答案 0 :(得分:3)

我用过

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

按下按钮。

我创建了这样的按钮:

UIButton *detailButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
detailButton.frame = CGRectMake(0, 0, 44, 44);
detailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
detailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
annotationView.rightCalloutAccessoryView = detailButton;

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

答案 1 :(得分:1)

看看我的代码示例:

- (MKAnnotationView *)mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>)annotation
{   
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    TagMark * tempMark = (TagMark *) annotation;

    MKPinAnnotationView *dropPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"venues"];

    dropPin.animatesDrop = YES;
    dropPin.canShowCallout = YES;

    UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeCustom];
    pinButton.frame = CGRectMake(2, 2, 28, 29);
    [pinButton setImage:[UIImage imageNamed:@"ph.png"] forState:UIControlStateNormal];
    pinButton.tag = [[tempMark useID] intValue];
    [pinButton addTarget:self action:@selector(makeCall:) forControlEvents:UIControlEventTouchUpInside];

    dropPin.rightCalloutAccessoryView=pinButton; 

    return dropPin;
}

希望它对你有所帮助......

答案 2 :(得分:0)

要在默认蓝点的annotationView中添加按钮,请使用委托方法并按以下方式添加按钮:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if (!view.rightCalloutAccessoryView) {

    UIButton *detailButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
    view.rightCalloutAccessoryView = detailButton;

    detailButton.frame = CGRectMake(0, 0, 44, 44);
    detailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    detailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

 }
}