注释右配件按钮不会改变

时间:2014-06-01 17:37:49

标签: ios objective-c mapkit

我的注释会触发正确的操作,但无论我选择哪个按钮,它们都不会显示正确的附件按钮。以下是我的代码。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

    static NSString *s = @"ann";
    MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
    if (annotation != self.mapView.userLocation) {
        pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
        pin.canShowCallout = YES;

        pin.calloutOffset = CGPointMake(0, 0);

        UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pin.rightCalloutAccessoryView = button;
        [button addTarget:self
                   action:@selector(getDirections:)
         forControlEvents:UIControlEventTouchUpInside];
    }
    return pin;
}

1 个答案:

答案 0 :(得分:0)

试试这个:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
        // If it's the user location, just return nil.
        if ([annotation isKindOfClass:[MKUserLocation class]])
            return nil;

        // Handle any custom annotations.
        MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ann"];
        if(pin){
        pin.annotation = annotation;
    } else {
        pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ann"];
    }

    pin.canShowCallout = YES;
    pin.calloutOffset = CGPointMake(0, 0);

    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pin.rightCalloutAccessoryView = button;
    [button addTarget:self
               action:@selector(getDirections:)
     forControlEvents:UIControlEventTouchUpInside];

    return pin;
}