我想在注释视图中添加一个按钮并为其指定一个动作。我有点想通了,只有我的代码有一个小问题。
// 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
功能不起作用。只有在我取消选择注释并再次触摸它才能正常工作。你能帮我推算我的代码吗?谢谢。
答案 0 :(得分: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;
}