ios - mkannotation View 中的按钮操作不起作用?

标签 ios objective-c iphone mkmapview mkannotationview

您好,我遇到了一种情况,在 map 上我设法显示了自定义注释。当注释被点击时,我必须显示一个包含一些信息和一个按钮的 View 。当用户点击按钮时,应显示一个警报。

代码如下

 - (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]; 

没有被调用。 我的屏幕看起来像这样

enter image description here

提前致谢

最佳答案

您需要为此按钮设置框架。这在代码中并没有真正看到。根据需要通过 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];

编辑:

找到了只能点击呈现注释的区域的原因。

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

Custom Callout Demo

enter image description here

关于ios - mkannotation View 中的按钮操作不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27519517/

相关文章:

iphone - 使用 UIKit View 操作时,整体 View 层次结构如何变化?

使用 Firebase 数据消息终止应用程序时未收到 iOS 推送通知

objective-c - 在 View 上显示图像 1 秒

ios - 添加子类 UIViewController 不允许在 .h 或 .m 中进行任何导入

iphone - 等待多个异步网络调用,然后对其结果执行操作

iphone - ios - 获取 NSString 的一部分

iphone - 将 NSMutableArray 保存在 NSUserDefaults 中

ios - Xcode 用户定义的build设置,值中包含空格

ios - 在 NSoperationQueue 上调用 -(void) cancelAllOperations 不会设置队列中存在的 NSOperation 的 isCancelled 属性

objective-c - 在某些 View 中捕获屏幕上任意位置的触摸事件?