检测所选的地图注释Xcode

我有一个地图和多个注释掉在。 当我select一个注释,并点击揭示button一个新的视图出现(DetailViewController),其中包含一个表视图。 我想要的是,当DetailViewController打开时,直接select注释的描述。

这是地图和注释,当我标签的出现显示按钮图像2出现,我想选定的注释说明成为选择

在这里输入图像说明

您可以使用下面的代码来检测在MKMapView中selectMKAnnotation的时间

@interface ClassVC () { int notationTag; } - (void)viewDidLoad { notationTag = 0; //initialisation of variable } - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { // If it's the user location, just return nil. if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; // Handle any custom annotations. if ([annotation isKindOfClass:[MKPointAnnotation class]]) { // Try to dequeue an existing pin view first. MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"]; if (!pinView) { // If an existing pin view was not available, create one. pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"]; pinView.canShowCallout = YES; pinView.image = [UIImage imageNamed:@"mallicon.png"]; pinView.calloutOffset = CGPointMake(0, 32); //pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; pinView.tag = notationTag; } else { pinView.annotation = annotation; } notationTag++; return pinView; } return nil; } 

然后在calloutAccessoryControlTapped中,

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { ContactDetails *contact=[[ContactDetails alloc] initWithNibName:@"ContactDetails" bundle:nil]; contact.name1Str = [NSString stringWithFormat:@"%d",view.tag]; } 

在下一个视图中添加这一行代码

 NSIndexPath *index =[NSIndexPath indexPathWithIndex:[name1Str intValue]]; [tableView selectRowAtIndexPath:index animated:NO scrollPosition:UITableViewScrollPositionMiddle]; 

在我的情况下,我用下面的代码来重新加载,你也可以尝试

 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { self.mapView.centerCoordinate = view.annotation.coordinate; NSLog(@"======Tag====%ld", (long)view.tag); NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[view tag] inSection:0]; [self.collectionView scrollToItemAtIndexPath:rowToReload atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO]; } 

使用委托方法didSelectAnnotationView:并将detalViewController推入您的导航控制器

这将有所帮助。

使用这个委托方法:

 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ AnnotationView *annotation = (AnnotationView *)mapView.annotation; NSInteger *yourIndex = annotation.myindex; } 

并推动您的导航控制器