iOS - MKAnnotationView自定义UIButton问题

时间:2013-12-27 06:36:59

标签: 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

但是,当按下 rightside 控制按钮时,按钮移动到

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

除了添加 centre 之外,点击rightCalloutAccessoryView时是否无法获取操作?

非常感谢任何帮助。

感谢。

2 个答案:

答案 0 :(得分:1)

试试这个。

-(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);
    }

答案 1 :(得分:0)

(i)中。首先尝试使用圆角矩形按钮,然后使用自定义按钮 (ⅱ)。然后,检查图像名称是否与资源文件夹中的名称相同