单击 DetailDesclosure 按钮时,MKAnnotationView 推送到视图控制器 [英] MKAnnotationView Push to View Controller when DetailDesclosure Button is Clicked

查看:16
本文介绍了单击 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!

推荐答案

如果您正在使用故事板并且有从当前场景到目标场景的转场,您只需响应 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];
}

显然,您可以检查与该 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];
    }
}

<小时>

如您所见,我将 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天全站免登陆