【发布时间】:2014-03-13 05:33:23
【问题描述】:
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 中。
【问题讨论】:
标签: objective-c ios mkmapview mkannotationview