Search code examples
iphonemkmapviewmkannotationview

MKMapView MKAnnotationView ISSUE?


I have a map view with pins that when the user selects a pin it goes to a detail screen for that pin. I also have a table view that when the user selects an item it goes to the same type detail view.

I am adding the multiple pins in mapview through array. I have added button to the accessory view,

Here's the problem ...

when I tap the button pin selection takes him to the detail view for a different pin. But in the table view it still works fine..anyone pls help me with this...?

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{
    MKPinAnnotationView *newAnnotation = nil;
    if([annotation.title isEqualToString:@"You are here."])
    {
        newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"greenpin"];
        newAnnotation.pinColor = MKPinAnnotationColorGreen;
    }
    else
    {      
        newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"redpin"];
        newAnnotation.pinColor = MKPinAnnotationColorRed;
        UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinButton.tag = indexvar;
        [newAnnotation addSubview:pinButton];
        newAnnotation.canShowCallout = YES;    
        newAnnotation.rightCalloutAccessoryView = pinButton;
        newAnnotation.calloutOffset = CGPointMake(-5, 5);
        indexvar++;
        return newAnnotation;
        [pinButton release];
    }
    newAnnotation.animatesDrop = YES;
    newAnnotation.canShowCallout = YES;
    return newAnnotation;
}

- (void)mapView:(MKMapView *)wikiMapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{
    NSInteger selectedIndex = ((UIButton *)control).tag;
    NSLog(@"Property Index : %d ",selectedIndex);
    DetailView = [[PropertyDetails alloc] initWithNibName:@"PropertyDetails" bundle:nil];
    DetailView.propertyReference = [PropertyReferenceArr objectAtIndex:selectedIndex];
    DetailView.propertyTitle = [propertyTitlesArr objectAtIndex:selectedIndex];
    DetailView.propertyPrice = [propertyPriceArr objectAtIndex:selectedIndex];
    DetailView.propertyBedrooms = [propertyBedroomsArr objectAtIndex:selectedIndex];
    DetailView.propertyLatitude = [propertyLatitudesArr objectAtIndex:selectedIndex];
    DetailView.propertyLongitude = [propertyLongitudesArr objectAtIndex:selectedIndex];
    DetailView.propertyDefaultImage = [PropertyImageArr objectAtIndex:selectedIndex];
    DetailView.propertyStatusId = [PropertyStatusArr objectAtIndex:selectedIndex];
    [self.navigationController pushViewController:DetailView animated:YES];         
    [DetailView release]; 
}

Solution

  • -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {    
        myAnnotation *annView = view.annotation; // myAnnotation is my custom class for annotations
        // now in annView you have the exact annotation whose accessorybutton is clicked now you can do whatever you want with it.
        NSLog(@"title = %@",annView.title);
    }
    

    Rather then setting the button tag you can use the title or subtitle of the annotation for futher comparision or whatever you want

    In your code you are using

    [newAnnotation addSubview:pinButton]; // i think this is wrong you should only add it to accessory view