mkannotation视图中的按钮操作无效? [英] Button action in mkannotation view not working?

查看:89
本文介绍了mkannotation视图中的按钮操作无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在地图上陷入了一种情况,我已设法显示自定义注释。点击注释时,我必须显示包含一些信息和一个按钮的视图。当用户点击按钮时,应显示一个警告。

Hello i have stuck in one situation on map where i have managed to show custom annotation. When the annotation is tapped i have to show a view with some information and one button. when user taps on the button then one alert should display.

代码如下

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

未被调用。
和我的屏幕看起来像这样

which is not getting called. and my screen looks like this

提前致谢

推荐答案

您需要设置此按钮的框架。这在代码中并未真正见到。根据需要通过x位置和y位置调整框架。

You need to set frame for this button. which is not really seen in the code. Adjust frame as you want by x position and y position.

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

修改:

找到理由 你只能点击渲染了注释的区域。

Found the reason you can only tap on area, in which annotation is rendered.

这是自定义呼叫的简单演示:

Here is simple demo for custom call out:

这篇关于mkannotation视图中的按钮操作无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆