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 了。

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘