【问题标题】:How Do I Determine Which MKAnnotation is clicked?如何确定单击了哪个 MKAnnotation?
【发布时间】:2013-02-28 20:54:53
【问题描述】:

我是 iphone 开发的菜鸟,我正在尝试确定注释的 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;    
}

【问题讨论】:

    标签: iphone dictionary indexing int mkannotation


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      编写以下代码获取索引值

      - (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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-15
        • 2014-02-04
        • 1970-01-01
        • 1970-01-01
        • 2012-01-11
        • 2011-03-18
        相关资源
        最近更新 更多