2011-09-16 53 views
4

當標籤附件按鈕時,我需要傳遞itemId,所以我可以識別要傳遞到我的詳細信息視圖的項目。傳遞id到calloutAccessoryControlTapped

到目前爲止:

添加註釋:

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); 
} 
+0

你能否提供詳細介紹MapViewAnnotation。我需要MapViewAnnotation.h&MapViewAnnotation.m的內容 –

回答

10

添加itemid財產到MapViewAnnotation類。

當添加註釋,設置屬性調用addAnnotation前:

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

calloutAccessoryControlTapped,訪問註釋詳情如下:

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

(如果itemid將是一個int,改變%@在NSLog到%d。)

+0

之前沒有注意到,但是在viewForAnnotation方法中,如果view正被重用,則需要設置註釋。所以它應該是:'if(pinAnnotation == nil)... else pinAnnotation.annotation = annotation;'否則,獲取重用視圖的註釋可能指向錯誤的註釋。 – Anna