iOS - MKAnnotationView 自定义 UIButton 问题

标签 ios uibutton mkmapview mkannotationview

将自定义 UIButton 添加到 MKAnnotationView 后,按钮的 UIControlEventTouchUpInside 不起作用。

请看下面我的代码:

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *annotationView = (MKAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"myIdentifier"];
    if (annotationView == nil) {
       annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.image = [UIImage imageNamed:@"location-pin"];

        UIButton *rightButton   =   [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton.frame       =   CGRectMake(0, 0, 150, 30);
        [rightButton setTitle:annotationText.userName forState:UIControlStateNormal];
        [rightButton setBackgroundColor:[UIColor clearColor]];
        rightButton.layer.borderColor = [UIColor blackColor].CGColor;
        rightButton.showsTouchWhenHighlighted   =   YES;
        [rightButton addTarget:self action:@selector(rightAcccoryViewButtonCLicked:) forControlEvents:UIControlEventTouchUpInside];
        annotationView.rightCalloutAccessoryView = rightButton;
    }
    else {
        annotationView.annotation = annotation;
    }
    return annotationView;
}
return nil;

}

rightButton 正在使用值正确初始化。但是当我点击中心 的按钮时,它没有被识别出来。 enter image description here

但是,当点击 右侧 的按钮时,控件会移动到

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

.

除了添加到 rightCalloutAccessoryView 之外,在 center 处点击时是否无法执行操作?

非常感谢任何帮助。

谢谢。

最佳答案

试试这个。

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {

    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[self.mapview dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
    if(pinView == nil)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"] ;
    }

 UIButton *detailButton = [[UIButton  alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
        [detailButton addTarget:self action:@selector(addToListButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [detailButton setSelected:NO];
        pinView.leftCalloutAccessoryView = detailButton;
        [pinView setImage:[UIImage imageNamed:@"locationPin.png"]];
        [pinView setFrame:CGRectMake(0, 0, 55, 55)];
        pinView.animatesDrop = NO;
        pinView.canShowCallout = YES;
       // [pinView setSelected:YES animated:YES];


    return pinView;

}

-(void)addToListButtonTapped:(id)sender
{
    int temp =2;
   AnnotationViewController *ann1 =[sender superview];
    NSString *str = ann1.title;
    NSLog(@"button tapped");
    NSLog(@"%@",str);
    }

关于iOS - MKAnnotationView 自定义 UIButton 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20796055/

相关文章:

ios - 在iOS 13中使用UISearchController时,状态栏在iOS中变为白色

ios - 到达 SLComposeServiceViewController::awakeFromNib 需要很长时间

swift - ImageView 和按钮未出现在模拟器中

ios - 显示不带 UIButton 或 UINavigationButton 的 UIMenu

ios - 导航到 MKMapView 上的注释位置

ios - 更改 mkmapview(删除注释)

ios - 在 swift 中使用 FBSession.FBSessionState 值

ios - 点击时 UIButton 不显示任何效果?

ios - MKMapView annotationContainer :viewForAnnotation 中持续崩溃

ios - 准备 CoreML 模型时出错 : "<something>" is not supported for CoreML code generation