我是iphone開發人員的noob,我試圖確定一個註釋的int值,所以我可以採取該int值並交叉引用它到一個數組來獲得相應的值。但是,當我嘗試使用.tag方法獲取int值時,該值始終返回爲零。如果我有5個annontations,我需要能夠確定哪個annontation是0,1,2,3和4.任何幫助,非常感謝。如何確定點擊哪個MKAnnotation?
我的代碼
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
if (view.selected==YES) {
NSInteger annotationIndex = view.tag; //Always returns zero
NSLog(@"annotationIndex: %i", annotationIndex);
}
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// Define your reuse identifier.
static NSString *identifier = @"MapPoint";
if ([annotation isKindOfClass:[MapPoint class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
NSInteger clickIndex = rightButton.tag; //Always returns zero, despite there being 5 annotations
NSLog(@"buttonIndex: %i", clickIndex);
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}
return nil;
}