如何将MyLocation对象从Mapview传递到tableview?

时间:2013-05-30 21:12:38

标签: ios uitableview mkannotation uistoryboardsegue

我有一个从coredata获取的mapview,并获取一个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;

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

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调用详细的tableview。我的问题是,我该如何处理将对象传递给详细的tableview?

2 个答案:

答案 0 :(得分:1)

performSegueWithIdentifierannotation保存到成员var之前的地图视图中,然后覆盖

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

答案 1 :(得分:0)

使用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];
}
}