【问题标题】:How can I get which annotation UIButtonTypeDetailDisclosure clicked in objective c?如何获取在目标 c 中单击了哪个注释 UIButtonTypeDetailDisclosure?
【发布时间】:2012-03-01 16:25:49
【问题描述】:

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

【问题讨论】:

    标签: objective-c ios map mkannotation


    【解决方案1】:

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

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

    【讨论】:

    • nslog 消息是 calloutAccessoryControlTapped: annotation = 是什么意思?
    • 我怎么知道点击了哪个按钮?
    • MKUserLocation 是系统为用户位置提供的注释(蓝点)。我不知道你是否有意,但你的viewForAnnotation: 将这个蓝点变成了一个红色的别针。我建议你阅读the documentation 以及参考Apple's sample codes
    猜你喜欢
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2014-06-21
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2012-05-23
    相关资源
    最近更新 更多