繁体   English   中英

单击DetailDesclosure按钮时,MKAnnotationView按下以查看控制器

[英]MKAnnotationView Push to View Controller when DetailDesclosure Button is Clicked

我想在我正在显示的地图上单击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?

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

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

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

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

当然,如果你想像往常一样将任何信息传递给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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM