objective-c - 如何获取在 objective-c 中单击的 UIButtonTypeDetailDisclosure 注释?

标签 objective-c ios dictionary mkannotation

我想在单击注释的详细信息按钮时进行详细 View 。但每个引脚必须有不同的细节 View 。为此,我尝试将标签设置为按钮。然后我将在 calloutAccessoryControlTapped 方法中使用此标签来添加其详细 View 。我的代码如下。但它停在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];
}

最佳答案

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

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

关于objective-c - 如何获取在 objective-c 中单击的 UIButtonTypeDetailDisclosure 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9206812/

相关文章:

c# - ResourceDictionary : {"Key cannot be null.\r\nParameter name: key"} 中的运行时错误

iOS7 Xcode 5 升级导致长按崩溃应用程序

ios - 序列中可以有 if 语句吗?

objective-c - 无法构建 Objective-C 模块 'ContactsUI'

c++ - 我需要什么类型的数据结构来链接到多个参数

python - 将嵌套列表的字典转换为 pandas DataFrame

iphone - 遍历 NSDictionary 获取 null 值

iOS 如何将图像索引从 collectionview 传递到 scrollview

ios - jasonett/iOS 的后退按钮

ios - 将图像从 map 注释传递到详细信息 View Controller