簡體   English   中英

iOS-MKAnnotationView自定義UIButton問題

[英]iOS - MKAnnotationView Custom UIButton issue

將自定義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已正確使用值進行初始化。 但是當我按下中間的按鈕時,它沒有被識別。 在此處輸入圖片說明

但是,當點擊rightside的按鈕時,控件將移至

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

除了添加到rightCalloutAccessoryView之外,在centre rightCalloutAccessoryView是否無法rightCalloutAccessoryView

任何幫助深表感謝。

謝謝。

嘗試這個。

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

(一世)。 首先嘗試使用圓角矩形按鈕,然后使用自定義按鈕。
(ⅱ)。 然后,檢查圖像名稱是否與資源文件夾中的名稱相同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM