lianlianbushell 2013-08-09 03:15 采纳率: 0%
浏览 947

如何给detailVC发送数据

使用didSelectRowAtIndexPath:传递一些数据,从一个table视图控制器到一个detail视图控制器。

我现在要将数据从地图callOutAccessoryControlTapped:方法发送出去,但是不知道应该怎么实现。

应该怎么用callOutAccessoryControlTapped:发送detailViewController.descriptionTextViewString = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"short_description"];

其中objectAtIndex:indexPath.row是不在地图callOutAccessoryControlTapped:方法中辨识的。

下面是didSelectRowAtIndexPath: 代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    ScrollView_ExampleViewController *detailViewController = [[ScrollView_ExampleViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    detailViewController.latStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"address"] objectForKey:@"lat"];
    detailViewController.lngStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"address"] objectForKey:@"lng"];

    detailViewController.addressStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"address"] objectForKey:@"address"];
    detailViewController.titleStr = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"];

    detailViewController.mainImageUrl = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"image"];

    detailViewController.listingId = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"id"];

    detailViewController.descriptionTextViewString = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"short_description"];

    [self.navigationController pushViewController:detailViewController animated:YES];

}

下面是地图注释calloutAccessoryControlTapped:方法:

- (void)mapView:(MKMapView *)mv annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control {
    ScrollView_ExampleViewController *detailViewController = [[ScrollView_ExampleViewController alloc] initWithNibName:@"ScrollView_ExampleViewController" bundle:nil];

    MyAnnotation *theAnnotation = (MyAnnotation *) pin.annotation;

    detailViewController.titleStr = theAnnotation.title;
    detailViewController.addressStr = theAnnotation.subtitle;
    //   detailViewController.latStr = theAnnotation.latString;
    // detailViewController.lngStr = theAnnotation.lngString;

    //  detailViewController.url = theAnnotation.theUrl;

    [self.navigationController pushViewController:detailViewController animated:YES];
}
  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-01-12 09:38
    关注

    在 callOutAccessoryControlTapped: 方法中,你可以使用相同的方式来传递数据给 detailViewController,就像 didSelectRowAtIndexPath: 方法中所做的那样。但是因为这个方法中并没有indexPath这个参数,所以不能使用 objectAtIndex:indexPath.row 的方法来获取数据。


    解决方案是,你可以在添加 MyAnnotation 的时候,将需要传递给 detailViewController 的数据附加到 MyAnnotation 对象中。然后在 callOutAccessoryControlTapped: 方法中,获取 MyAnnotation 对象中的数据并传递给 detailViewController。


    例如,你可以在 MyAnnotation 中添加属性 descriptionTextViewString,然后在添加 MyAnnotation 到地图时将数据赋值给这个属性,在 callOutAccessoryControlTapped: 方法中获取该属性并传递给 detailViewController.descriptionTextViewString。

    (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
        for (MKAnnotationView *aV in views) {
            //check if annotation is of class MyAnnotation
            if ([aV.annotation isKindOfClass:[MyAnnotation class]]) {
                MyAnnotation *myAnnotation = (MyAnnotation *)aV.annotation;
                myAnnotation.descriptionTextViewString = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"short_description"];
            }
        }
    }
    

    在callOutAccessoryControlTapped: 方法中

    MyAnnotation *theAnnotation = (MyAnnotation *) pin.annotation;
    detailViewController.descriptionTextViewString = theAnnotation.descriptionTextViewString;
    

    这样就可以在callOutAccessoryControlTapped: 方法中传递descriptionTextViewString 这个数据给 detailViewController 了。

    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗