8

我想在我正在显示的地图上单击 DetailDisclosure 时切换视图。我当前的代码如下:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
    DetailViewController *detailViewController = [[DetailViewController alloc] 
    initWithNibName:@"DetailViewController" bundle:nil];
    detailViewController.title = dictionary[@"placeLatitude"]
    [self.navigationController pushViewController:detailViewController animated:YES];
}

我可以用这个推送到视图控制器,但我还没有弄清楚如何强制它从用于首先生成地图的 JSON 数组中提取详细信息。我正在提取这样的数据来生成地图:

 for (NSDictionary *dictionary in array)
 {
    // retrieve latitude and longitude from the dictionary entry

    location.latitude = [dictionary[@"placeLatitude"] doubleValue];
    location.longitude = [dictionary[@"placeLongitude"] doubleValue];

   //CAN I LOAD THE TITLE/ID OF THE LOCATION HERE?

我知道我有点偏离目标。也许只是朝正确的方向踢一脚可能会有所帮助。谢谢!

4

1 回答 1

11

如果您正在使用故事板并且从当前场景到目标场景有一个转场,您可以只响应calloutAccessoryControlTapped. 例如:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"Details" sender:view];
}

view显然,如果您有不同的 segue 要调用不同的注释类型,则可以检查与 that 关联的注释类型等。

当然,如果您想像prepareForSegue往常一样将任何信息传递给下一个场景。

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

如您所见,我将annotation对象传递给下一个视图。如果您的 JSON 结构中有与每个注释关联的其他字段,一个简单的解决方案是将属性添加到与您要跟踪的每个字段关联的自定义视图中。只需转到您的注释自定义类的 .h 并添加您需要的属性。然后,当您创建自定义注释(要添加到地图中)时,也只需设置这些属性。然后,当您将此注释传递给下一个视图控制器时,您所需的所有属性都将在那里可用。


显然,如果您使用的是 NIB,只需执行您想要的任何 NIB 等效项来实例化下一个视图控制器,设置您想要的任何属性,然后推送到它或以模态方式呈现它,例如:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    DetailsViewController *controller = [[DetailsViewController alloc] initWithNibName:nil
                                                                                bundle:nil];
    controller.annotation = annotationView.annotation;
    [self.navigationController pushViewController:controller animated:YES]; // or use presentViewController if you're using modals
}
于 2013-02-11T04:51:53.607 回答