2012-02-28 62 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