0

我是 iphone developmentoment 的菜鸟,我正在尝试确定注释的 int 值,因此我可以获取该 int 值并将其交叉引用到数组以获得相应的值。但是,当我尝试使用 .tag 方法获取 int 值时,该值始终返回为零。如果我有 5 个注释,我需要能够确定哪个注释是 0、1、2、3 和 4。非常感谢任何帮助。

我的代码

- (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;    
}
4

2 回答 2

1

tag 属性是你必须设置的。我认为你没有在任何地方设置它。

当然对于您使用 buttonWithType 创建的 UIButton。默认标记值为 0,因此在创建视图(按钮)后,您总是会得到 0 来请求标记。

于 2013-02-28T22:58:59.427 回答
0

编写以下代码以获取索引值

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
  // Annotation is your custom class that holds information about the annotation
  if ([view.annotation isKindOfClass:[Annotation class]]) {
    Annotation *annot = view.annotation;
    NSInteger index = [self.arrayOfAnnotations indexOfObject:annot];
  }
}

在我之前的帖子中 https://stackoverflow.com/a/34742122/3840428

于 2016-03-15T09:21:07.997 回答