如何在annotationView上设置按钮以在iPhone中执行操作?

发布于 2024-12-17 08:21:55 字数 1139 浏览 1 评论 0原文

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

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

谁能告诉我如何将按钮添加到默认用户位置的蓝点注释视图中?提前致谢。

I want to add button in annotationView in mapkit in iphone for performing an action on clicking that button. How can i perform action on clicking that button? i also dont able to add button in annotation view using the following code:

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

Can anyone please tell me how can i add button to the default userlocation's blue dot's annotation view? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

衣神在巴黎 2024-12-24 08:21:55

我曾经

- (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

I used

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

to capture the button press.

And I created the button like this:

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

in

-(MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation: (id<MKAnnotation>)annotation
最美不过初阳 2024-12-24 08:21:55

只需看看我的代码示例:

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

希望它能帮助您......

Just look at my code sample :

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

Hope it helps you...

打小就很酷 2024-12-24 08:21:55

要在默认蓝点的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;

 }
}

For adding the button in default blue dot's annotationView, use the delegate method and add button there in following way:

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

 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文