Nico AD
Nico AD

Reputation: 1709

click on button in MKAnnotationView

I m working on an app using MKMapView

there's some annotation and when u click on the annotation image , details are shown

for that i ve build my custom @interface Annotation : NSObject and added a boolean value "bClicked"

now I want the user to be able to click on a detail button inside the detailview but the code i ve made is not working :

#pragma mark - MKMapViewDelegate

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKAnnotationView *annotationView = nil;


    int index = 0;

    BOOL bFound = FALSE;

    for (int i=0;i<[shopAnnotations count];i++)
    {
        if (annotation == [shopAnnotations objectAtIndex:i])
        {
            index = i;
            bFound = TRUE;
            break;
        }
    }

    if (bFound==FALSE)
    {
        //ADLog(@"bFound FALSE annotation %p",annotation);
        return annotationView; // <-- nil
    }


    Annotation *shopAnnotation = [shopAnnotations objectAtIndex:index];

    annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:shopAnnotation.identifier];

    if (!annotationView)
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:shopAnnotation reuseIdentifier:shopAnnotation.identifier] autorelease];

    for (UIView*view in annotationView.subviews)
    {
        [view removeFromSuperview];
    }

    annotationView.image = shopAnnotation.Image;
    annotationView.alpha = 1.0f;
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;

    annotationView.userInteractionEnabled = YES;

    if (shopAnnotation.bClicked==TRUE)
    {

        UIView *detailView = [[UIView alloc ] initWithFrame:CGRectMake(0, -50, 290, 56)];
        detailView.userInteractionEnabled = YES;
        detailView.superview = annotationView;
        [detailView release];

        UIImageView *bg = [[UIImageView alloc ] initWithImage:[UIImage imageNamed:@"bulle_carte.png"]];

        [bg setFrame:CGRectMake(0, 0, 290, 56)];
        bg.superview = detailView;

        [bg release];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        [button setFrame:CGRectMake(250, 3, 35, 35)];

        [button setImage:[UIImage imageNamed:@"fleche_bleue_d.png"] forState:UIControlStateNormal];
        button.tag = [shopAnnotation.identifier intValue];
        [button addTarget:self action:@selector(btnDetailPressed:) forControlEvents:UIControlEventTouchUpInside];

        button.superview = detailView;

        button.userInteractionEnabled = YES;

        [annotationView setRightCalloutAccessoryView:detailView];

    }


return annotationView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view    calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"calloutAccessoryControlTapped");
}

-(void)btnDetailPressed:(id)sender
{
NSLog(@"btnDetailPressed");
}

when I click on the button or inside the DetailView, btnDetailPressed or calloutAccessoryControlTapped are not called/triggered.

Any suggestions?

Upvotes: 2

Views: 1985

Answers (1)

james075
james075

Reputation: 1278

Use the MKMapViewDelegate's method mapView:didSelectAnnotationView::

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[YOUR_CUSTOM_CLASS class]]) {
        //customize it
    }
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    if ([view isKindOfClass:[YOUR_CUSTOM_CLASS class]]) {
        //do something
    }
}

Apple's doc

Upvotes: 3

Related Questions