2012-02-28 61 views
0

我想添加一個按鈕到註釋視圖併爲其分配一個操作。我有種想法,只有我的代碼有一個小問題。現在將細節按鈕添加到MKAnnotationView

// so when I touch the pin , and the annotation view is displayed , I create a button and add it to the view 

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
    { 

     view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    } 
    // when the button is pressed , go to another view 

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
    { 
     NSLog(@"calloutAccessoryControlTapped"); 
     ArticleViewController *det = [[ArticleViewController alloc]init]; 
     det.leTitle = view.annotation.title; 
     det.link = @"http://en.wikipedia.org/wiki/San_Francisco"; 
     [self.navigationController pushViewController:det animated:YES]; 

    } 

,問題是,我第一次接觸到的註釋和創建按鈕, 的

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 

功能不起作用。只有在取消選擇註釋並再次觸摸它後,它才能正常工作。你能幫我提供我的代碼嗎?謝謝。

回答

6

找到了!

我只是不得不在一個函數中加入一個函數,該函數在我第一次使用該函數之前調用。

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKPinAnnotationView *pinAnnotation = nil; 
    if(annotation != mapViewHandler.userLocation) 
    { 
     static NSString *defaultPinID = @"myPin"; 
     pinAnnotation = (MKPinAnnotationView *)[mapViewHandler dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (pinAnnotation == nil) 
      pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; 

     pinAnnotation.canShowCallout = YES; 

     //instatiate a detail-disclosure button and set it to appear on right side of annotation 
     UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     pinAnnotation.rightCalloutAccessoryView = infoButton; 

    } 

    return pinAnnotation; 
} 
+0

如果您的問題解決了,請不要忘記接受您的答案。 – 2012-02-28 09:15:51