【问题标题】:Get indexPath of mapView annotation获取mapView注解的indexPath
【发布时间】:2013-10-29 13:14:14
【问题描述】:

When an annotation on the map is selected and the callout accessory control button is tapped I need to be able to find the index of that annotation.索引将用于查看数组中的信息,为下一个视图控制器做准备(详细)。

如何找到注解的索引?

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"Button clicked");
[self performSegueWithIdentifier:@"showDetail" sender:self];
}

谢谢

【问题讨论】:

  • 尝试为每个Annotation设置标签。

标签: ios objective-c mkmapview mkannotationview mkpinannotationview


【解决方案1】:

MKAnnotationView * 视图 = ...

NSUInteger index = [mapView.annotations indexOfObject:view.annotation];

【讨论】:

  • 这将在地图视图的注释列表中给出注释的索引,但假设位置与自己的注释数组匹配是不安全的。正如@nevan king 建议的那样,使用已经嵌入(或引用)所需数据的自定义注释类是首选方法。
  • 我创建了一个自定义注释类,现在存储描述。如果我这样做 NSLog(@"%@", mapView.annotations.description);它打印出所有注释的描述。我如何只为选定的那个做。
【解决方案2】:

它没有像表格视图中那样的索引路径。使用您自己的模型类作为注释,使其符合MKAnnotation,然后使用您拥有的任何逻辑来跟踪单个模型项。或者只是将模型项按原样传递给下一个视图控制器。

【讨论】:

  • 你有例子吗?我不明白你的回答。
  • 您要添加到地图中的事物的类别是什么?即你有“商店”类或类似的东西吗?
  • 我有一个数组存储多个对象,其中包含标题、副标题、描述和地址。我有一个 for 循环,它遍历地址元素的数组并将其绘制在地图上。我希望能够在我的下一个视图控制器上显示该数组索引的描述。
  • 那么数组中的任何东西都是你的模型。使它成为一个真正的类并在 .h 文件中添加<MKAnnotation>。查看 Apple 地图标注示例代码,了解将内容打包到注释中的示例。
猜你喜欢
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
  • 2023-04-01
相关资源
最近更新 更多