将变量从 MKAnnotationView 传递到 segue

passing variable from MKAnnotationView to segue

我有一个 MKMapView,我从数组中放置了一些引脚。每个地图图钉都有一个 MKAnnotationView,link 连接到一个 segue 和另一个控制器。

现在所有这些都运行良好,但我看不到如何通过 MKAnnotationView 将变量传递到 segue。

我可以传递字符串或整数,因为我可以将用户名作为字符串传递,或者传递数组的 id 并在 segue 之后的第二个视图控制器中使用它。

我循环遍历我的数组、删除引脚、添加注释然后调用 segue 的工作代码如下。

一切正常,填充地图,删除箱子,注释工作,link 到新的 segue。这一切都很完美!

我只需要能够将变量从地图引脚数组传递到第二个视图控制器 segue。

- (void)loadMapPins {
  for (int i=0; i<maparray.count; i++){
    Location *item = _feedItems1[i];
    CLLocationCoordinate2D myCoordinate = {[item.latitude doubleValue], [item.longitude doubleValue]};
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = myCoordinate;
    point.title = item.firstname;
    point.subtitle = [NSString stringWithFormat: @"%@", item.username];
    [self.mapView addAnnotation:point];
  }
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {
  if (annotation == self.mapView.userLocation) return nil;
  static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
  MKPinAnnotationView* customPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
  customPin.pinColor = MKPinAnnotationColorRed;
  customPin.animatesDrop = YES;
  customPin.canShowCallout = YES;
  UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
  customPin.rightCalloutAccessoryView = rightButton;
  return customPin;
}

- (IBAction)showDetails:(id)sender {
  [self performSegueWithIdentifier:@"showProfileFromMap" sender:self];
}

你应该使用这个MKMapViewDelegate方法

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    CustomAnnotationClass *annotation = view.annotation;
    selectedAnnotationVariable = annotation.yourProperty;
    [self performSegueWithIdentifier:@"performSegue" sender:self];
}