【问题标题】:How do I pass MyLocation object from Mapview to tableview?如何将 MyLocation 对象从 Mapview 传递到 tableview?
【发布时间】:2013-05-26 14:33:29
【问题描述】:

我有一个从 coredata 获取并获取 Location 对象数组的地图视图。位置对象是自定义的 NSManagedObjects,如下所示:

@property (nonatomic, retain) NSString * ciudad;
@property (nonatomic, retain) NSString * horario;
@property (nonatomic, retain) NSString * codigo;
@property (nonatomic, retain) NSString * nombrePublico;
@property (nonatomic, retain) NSString * telefono;
@property (nonatomic, retain) NSString * direccion;
@property (nonatomic, retain) NSString * coordenadas;
@property (nonatomic, retain) NSString * hrs24;
@property (nonatomic, retain) NSString * driveThru;
@property (nonatomic, retain) NSDate * updatedAt;

然后我循环遍历数组中的所有位置对象并创建注释,如下所示:

MyLocation *annotation = [[MyLocation alloc] initWithName:storeDescription address:address coordinate:coordinate distance:0];

然后我将这些注释添加到一个数组中,然后通过按一下按钮将它们绘制出来:

- (IBAction)refreshTapped:(id)sender {
    for (id<MKAnnotation> annotation in _mapView.annotations) {
        [_mapView removeAnnotation:annotation];
    }
    for (MyLocation *annotation in self.myLocationsToSort) {
        [_mapView addAnnotation:annotation];   
    }   
}

最后,一旦用户点击标注上的披露按钮,我想将对象传递给 UIViewController。所以首先我创建我的注释:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

那么我会这样做:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{

    if (![view.annotation isKindOfClass:[MyLocation class]])
        return;

    [self performSegueWithIdentifier:@"DetailVC" sender:view];

}

segue 调用详细表视图。我的问题是,我应该如何处理将对象传递给详细表视图?

【问题讨论】:

    标签: ios uitableview mkannotation uistoryboardsegue


    【解决方案1】:

    performSegueWithIdentifier 之前的地图视图中,将annotation 保存到成员变量然后覆盖

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"DetailVC"]) {
            [[segue destinationViewController] setAnnotation:self.annotation];
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用 prepareForSegue 方法并传递它:

      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
      {
      if ([[segue identifier] isEqualToString:@"DetailVC"])
      {
          YourViewController *vc = [segue destinationViewController];
      
          // Pass objects to the destination
          [vc setAObjectHere:object];
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2010-11-05
        • 2021-01-14
        • 1970-01-01
        • 1970-01-01
        • 2018-05-14
        相关资源
        最近更新 更多