如何在目标c中找到UIButtonTypeDetailDisclosure中的哪个注释?

时间:2012-02-09 07:14:35

标签: objective-c ios map mkannotation

我想在点击注释的详细信息按钮时进行详细视图。但每个引脚必须具有不同的细节视图。为此,我尝试将标签设置为按钮。然后我将在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];
}

1 个答案:

答案 0 :(得分:2)

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

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