【问题标题】:passing variable from MKAnnotationView to segue将变量从 MKAnnotationView 传递到 segue
【发布时间】:2015-07-08 15:27:37
【问题描述】:

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

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

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

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

这一切都很好,填充地图,删除垃圾箱,注释工作并链接到新的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];
}

【问题讨论】:

    标签: objective-c mkmapview mkannotationview


    【解决方案1】:

    你应该使用这个MKMapViewDelegate方法

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

    【讨论】:

    • 感谢您的提示。这看起来非常好,使用它,我设法传递了注释视图的标题或副标题。有什么方法可以发送在地图图钉标注上不可见的字段?似乎标题和副标题是我唯一可以通过的两项?我希望能够传递地图图钉上未显示的 ID 参考。
    • 我编辑了我的答案。您想从视图中获取注释,而不是直接从注释中获取所需的任何属性。希望这会有所帮助!
    • 我试图添加您编辑的代码,但我收到错误“未知类型名称‘MKAnnotation’;你的意思是‘MKAnnotationView’吗?”。我做错了吗?
    • 现在终于可以正常使用了!!感谢您的耐心和帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2017-06-03
    • 2013-06-10
    • 2020-02-25
    • 2021-09-12
    相关资源
    最近更新 更多