如何获取在目标 c 中单击的 UIButtonTypeDetailDisclosure 注释?

发布于 2025-01-03 12:26:10 字数 1590 浏览 2 评论 0原文

我想在单击注释的详细信息按钮时进行详细视图。但每个引脚必须有不同的细节视图。为此,我尝试将标签设置为按钮。然后我将在 calloutAccessoryControlTapped 方法中使用此标签来添加其详细视图。我的代码如下。但它停在for循环处。我该如何解决这个问题?

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
static NSString *annReuseId = @"test";

MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annReuseId];
if (annView == nil)
{
    annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annReuseId];

    annView.animatesDrop = YES;
    annView.canShowCallout = YES;
    [annView setSelected:YES];
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annView.rightCalloutAccessoryView=detailButton;
    for (int i=0; i<=[[mapView annotations] count]; i++)
    {
        detailButton.tag =[[[mapView annotations] objectAtIndex: i] integerValue];
        NSLog(@"button %i",detailButton.tag);

    }
}
else {
    annView.annotation = annotation;

}

return annView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{


NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
RestaurantsView *detailView=[[RestaurantsView alloc] initWithNibName:@"RestaurantsView" bundle:nil];
   //here, can set annotation info in some property of detailView
   // [[self navigationController] pushViewController:detailView animated:YES];
   // [detailView release];
[self.view addSubview:detailView.view];
}

I want to do detail view when click the annotation's detail button. But each pin must be have different detail view. To do this, I try to set tag to buttons. And then I will use this tags at calloutAccessoryControlTapped method for add their detail view. My code below. But it is stopped at for loop. How can I solve this?

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
static NSString *annReuseId = @"test";

MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annReuseId];
if (annView == nil)
{
    annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annReuseId];

    annView.animatesDrop = YES;
    annView.canShowCallout = YES;
    [annView setSelected:YES];
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annView.rightCalloutAccessoryView=detailButton;
    for (int i=0; i<=[[mapView annotations] count]; i++)
    {
        detailButton.tag =[[[mapView annotations] objectAtIndex: i] integerValue];
        NSLog(@"button %i",detailButton.tag);

    }
}
else {
    annView.annotation = annotation;

}

return annView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{


NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
RestaurantsView *detailView=[[RestaurantsView alloc] initWithNibName:@"RestaurantsView" bundle:nil];
   //here, can set annotation info in some property of detailView
   // [[self navigationController] pushViewController:detailView animated:YES];
   // [detailView release];
[self.view addSubview:detailView.view];
}

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

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

发布评论

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

评论(1

未央 2025-01-10 12:26:10

您不需要设置详细信息按钮的标签。您可以删除 viewForAnnotation: 中的整个 for 循环。如果您想访问 calloutAccessoryControlTapped: 中的注释,只需使用 view.annotation 就像您对 NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);

如果您确实需要索引,请使用 [mapView.annotations indexOfObject:view.annotation] 但要小心,因为该数组还可能包含所有其他类型的注释,例如 MKUserLocation

You don't need to set the tag of the detail button. You can remove the whole for loop in the viewForAnnotation:. If you want to access the annotation inside calloutAccessoryControlTapped: just use the view.annotation like you did with your NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);.

If you really need the index then use [mapView.annotations indexOfObject:view.annotation] but be careful since that array may also include all other kinds of annotations such as the MKUserLocation.

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