【问题标题】:Button action in mkannotation view not working?mkannotation 视图中的按钮操作不起作用?
【发布时间】:2015-02-15 15:25:45
【问题描述】:

您好,我在地图上遇到了一种情况,我设法显示了自定义注释。当注释被点击时,我必须显示一个带有一些信息和一个按钮的视图。当用户点击按钮时,应该会显示一个警报。

代码如下

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

没有被调用。 我的屏幕是这样的

提前致谢

【问题讨论】:

  • 您可以将按钮作为子视图添加到您的 bgview。还添加 [view sizeToFit];以及您想要在按钮单击时执行的操作,您可以在 didSelectAnnotation 中编写该代码

标签: ios objective-c iphone mkmapview mkannotationview


【解决方案1】:

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

【讨论】:

  • 我已经编辑了我的问题,但仍然调用了 didDeselectAnnotationView 方法而不是按钮操作
  • @ismail - 为您提供的示例演示。
猜你喜欢
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
相关资源
最近更新 更多