单击DetailDesclosure按钮时,MKAnnotationView按下以查看控制器 [英] MKAnnotationView Push to View Controller when DetailDesclosure Button is Clicked

查看:86
本文介绍了单击DetailDesclosure按钮时,MKAnnotationView按下以查看控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to switch views when a DetailDisclosure is clicked on a map I am displaying. My current code is as below:

- (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数组中提取细节。我正在提取这样的数据来生成地图:

I can push to the view controller with this, but I haven't figured out how to force it to pull details from the JSON array used to generate the map in the first place. I am pulling data like this to generate the map:

 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?

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

I know I'm a bit off target. Maybe just a kick in the right direction might help. Thank you!

推荐答案

如果您正在使用故事板并且从当前场景到目标场景有一个segue,您只需回复到 calloutAccessoryControlTapped 。例如:

If you are using storyboards and have a segue from your current scene to your destination scene, you can just respond to calloutAccessoryControlTapped. For example:

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

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

Obviously, you can check for the type of annotation associated with that view, etc., if you have different segues you want to call for different annotation types.

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

And, of course, if you want to pass any information to that next scene in prepareForSegue like usual.

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






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


As you can see, I pass the annotation object to my next view. If you have additional fields in your JSON structure that are associated with each annotation, one easy solution is to add properties to your custom view associated for each of the fields you want to track. Just go to the .h for your annotation custom class and add the properties you need. And then when you create your custom annotations (to be added to the map), just set these properties, too. Then, when you pass this annotation to the next view controller, all of your desired properties will be available there.

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

Clearly, if you're using NIBs, just do the NIB equivalents of whatever you want for instantiating the next view controller, setting whatever properties you want, and either pushing to it or presenting it modally, e.g.:

- (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
}

这篇关于单击DetailDesclosure按钮时,MKAnnotationView按下以查看控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆