我有一个具有 map View 的应用程序,它在 map 上显示了 20 个图钉(来自数组)。当用户点击它时,它会显示一个带有右侧附件按钮的气泡。 我的问题来了:我怎么知道按下了哪个引脚? 我听说了一些关于 mapView:didSelectAnnotationView 方法的事情,但我不太明白你如何获得 pin/callout 索引,以便我可以在我的数组的相同索引处获取对象的信息?感谢您的帮助!
最佳答案
当该方法被调用时——因为您的 viewController 类采用了 MKMapViewDelegate
,您可以调用-indexOfObject
在数组上并获取该引脚的索引(注释)。这是假设您的数组包含注释类的对象。
- (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];
}
}
如果您需要更多解释,我们需要知道您是如何添加这些引脚的,即 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
的实现方式.
关于ios - 如何保存选中的MKAnnotation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9848292/