2012-02-09 53 views
2

我想单击注解的详细按钮时执行详细视图。但是每个引脚必须具有不同的细节视图。要做到这一点,我尝试设置标签按钮。然后我会在calloutAccessoryControlTapped方法中使用这个标签来添加它们的详细视图。我的代码如下。但它停在for循环。我该如何解决这个问题?如何获取UIButtonTypeDetailDisclosure在目标c中单击的注释?

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

回答

2

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

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

+0

nslog消息是calloutAccessoryControlTapped:annotation = 什么是是什么意思? – 2012-02-09 07:37:33

+0

我怎么知道点击了哪个按钮? – 2012-02-09 07:40:13

+0

'MKUserLocation'是系统为用户位置提供的注释(蓝点)。我不知道你是否有意或无意,但是你的'viewForAnnotation:'把这个蓝点变成红色的针。我建议你阅读[文档](https://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html)以及参考[Apple的示例代码] (https://developer.apple.com/library/ios/#samplecode/MapCallouts/Listings/MapViewController_m.html) – 2012-02-09 08:10:35

相关问题