mkannotation视图中的button操作不起作用?

你好,我卡在地图上的一个情况,我已经设法显示自定义注释。 当点击注释时,我必须显示一些信息和一个button的视图。 当用户点击button时,应该显示一个警报。

代码如下

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { if (![view.annotation isKindOfClass:[MKUserLocation class]]) { POCustomAnnotation *annotation = (POCustomAnnotation *)[view annotation]; UIView *bgview=[[UIView alloc]initWithFrame:CGRectMake(-30, -150,130, 150)]; bgview.backgroundColor=[UIColor grayColor]; UILabel *templbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 130, 120)]; templbl.text=annotation.displayText; templbl.textAlignment=NSTextAlignmentRight; templbl.numberOfLines=10; [bgview addSubview:templbl]; UIButton *tembtn=[UIButton buttonWithType:UIButtonTypeCustom]; [tembtn setTitle:@"More info" forState:UIControlStateNormal]; [tembtn addTarget:self action:@selector(annotationBtnAction:) forControlEvents:UIControlEventTouchUpInside]; tembtn.backgroundColor=[UIColor grayColor]; tembtn.frame=CGRectMake(0, 121, 130, 30); [bgview addSubview:tembtn]; [view addSubview:bgview]; } else { POMapAnnotationView *callout = (POMapAnnotationView *)[[[NSBundle mainBundle]loadNibNamed:@"POMapAnnotationView" owner:self options:nil] objectAtIndex:0]; view.canShowCallout = NO; CGRect calloutViewFrame = callout.frame; calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width / 2 + 15, - calloutViewFrame.size.height); callout.frame = calloutViewFrame; [callout setLabelText:[self distanceText]]; [view addSubview:callout]; } } 

我正在试图解决这个问题

  [tembtn addTarget:self action:@selector(annotationBtnAction:) forControlEvents:UIControlEventTouchUpInside]; 

这是没有得到调用。 我的屏幕看起来像这样

在这里输入图像说明

提前致谢

您需要为此button设置框架。 这是代码中没有看到的。 通过x位置和y位置调整框架。

 UIButton *tembtn=[UIButton buttonWithType:UIButtonTypeCustom]; // Frame required to render on position and size. Add below line [tembtn setFrame:CGRectMake(0, 0, 100, 50)]; [tembtn setTitle:@"More info" forState:UIControlStateNormal]; [tembtn addTarget:self action:@selector(annotationBtnAction:) forControlEvents:UIControlEventTouchUpInside]; tembtn.backgroundColor=[UIColor grayColor]; [view addSubview:tembtn]; 

编辑:

find原因, 你只能点击区域,其中注释是呈现。

这里是自定义调用的简单演示:

自定义标注演示

在这里输入图像说明