将id传递给calloutAccessoryControlTapped

时间:2011-09-16 18:55:35

标签: iphone mkmapview

当勾选附件按钮时,我需要传递itemId,这样我就可以识别要传递给我的详细视图的项目。

到目前为止:

添加注释:

for (id row in self.detailItem) {
    Item *i = (Item *) row; 
    CLLocationCoordinate2D destination;
    destination.latitude = (double) i.latitude;
    destination.longitude = (double) i.longitude;

    //i.itemid

    MapViewAnnotation *destinationAnnotation = [[MapViewAnnotation alloc] initWithTitle: i.name andCoordinate: destination];
    [self.mapView addAnnotation: destinationAnnotation];     
    [destinationAnnotation release];
}

添加附件按钮

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
if(annotation != mapView.userLocation) 
{
    static NSString *defaultPinID = @"myPin";
    pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinAnnotation == nil )
        pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

    pinAnnotation.canShowCallout = YES;

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinAnnotation.rightCalloutAccessoryView = infoButton;

}

return pinAnnotation;
}

找到要传递给详细视图的项目

-(void)mapView:(MKMapView *)mapView annotationView:(MKPinAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
// get annotation details here.

NSLog(@"%@", control);
}

1 个答案:

答案 0 :(得分:10)

itemid属性添加到MapViewAnnotation类。

添加注释时,请在调用addAnnotation之前设置属性:

destinationAnnotation.itemid = i.itemid;
[self.mapView addAnnotation: ...

calloutAccessoryControlTapped中,访问注释详细信息,如下所示:

MapViewAnnotation *annotationTapped = (MapViewAnnotation *)view.annotation;
NSLog(@"annotationTapped.itemid = %@", annotationTapped.itemid);

(如果itemidint,请将NSLog中的%@更改为%d