I created a subclass of MKAnnotationView
which when selected display a callout view (similar to the MKPinAnnotationView) which is just a subclass of UIView
with a button and text for now. 标注被添加到注释视图中。我遇到的问题是我放在标注视图中的按钮从不触发任何动作,它似乎没有响应触摸事件。
我创建一个新的原因MKAnnotationView
是因为我需要在注释视图中显示多个文本和两个按钮,而我找不到通过常规MKPinAnnotationView
标注实现此目的的方法。
代码:
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation {
if(![annotation isKindOfClass:[CSMapAnnotation class]])
return nil;
CSMapAnnotation* csAnnotation = (CSMapAnnotation*)annotation;
CustomAnnotation *customAnnotationView=[[[CustomAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
MKPinAnnotationView* pin = nil; pin = [[[MKPinAnnotationView alloc] initWithAnnotation:customAnnotationView reuseIdentifier:@"identifier"] autorelease];
[pin setPinColor: MKPinAnnotationColorRed ];
MKAnnotationView *annotationView = pin;
annotationView.canShowCallout = YES;
[pin setPinColor:(csAnnotation.annotationType == CSMapAnnotationTypeStart) ? MKPinAnnotationColorGreen : ((csAnnotation.annotationType == CSMapAnnotationTypeCarLocation)? MKPinAnnotationColorPurple:MKPinAnnotationColorRed) ];
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,85,25)];
[leftView setBackgroundColor:[UIColor clearColor]];
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setBackgroundImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
[shareButton setFrame:CGRectMake(19+GAP, 0, 25, 25)];
[shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[leftView addSubview:shareButton];
UIButton *setAlertButton = [UIButton buttonWithType:UIButtonTypeCustom];
[setAlertButton setBackgroundImage:[UIImage imageNamed:@"alert.png"] forState:UIControlStateNormal];
[setAlertButton setFrame:CGRectMake(shareButton.frame.origin.x+shareButton.frame.size.width+GAP, 0, 25, 25)];
[setAlertButton addTarget:self action:@selector(alertButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[leftView addSubview:setAlertButton];
annotationView.rightCalloutAccessoryView = leftView;
[leftView release];
return annotationView;
}
-(void)shareButtonAction:(id)sender {
NSLog(@"%s",_cmd);
}
当我单击 shareButton 时,未调用 shareButtonAction 操作...
我也检查了上面的委托,但是当安装到 iPhone 3.0 时它没有被调用。如果我只有一个按钮来右标注,这个委托将调用,但是(在上述情况下)它在添加带有两个按钮的视图时不会调用。它发生在 iPhone 3.0 中。