2012-02-09 15 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

相關問題