質問

After adding a custom UIButton to MKAnnotationView, the UIControlEventTouchUpInside for the button is not working.

Please have a look at my code below :

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

}

The rightButton is getting initialised properly with value. But when I hit the button at centre, its not getting recognised. enter image description here

However, when the button is tapped at the rightside control moves to

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

.

Is it not possible to get the action when tapped at the centre other than adding to rightCalloutAccessoryView ?

Any help is much appreciated.

Thanks.

役に立ちましたか?

解決

Try this.

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

他のヒント

(i). First try using a rounded rectangle button, then use the custom button.
(ii). then, check the image name is same as the name in the resource folder

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top