【问题标题】:MKAnnotationView Push to View Controller when DetailDesclosure Button is Clicked单击 DetailDesclosure 按钮时,MKAnnotationView 推送到视图控制器
【发布时间】:2013-01-26 04:32:53
【问题描述】:

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

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

【问题讨论】:

    标签: iphone ios objective-c xcode mkmapview


    【解决方案1】:

    如果您正在使用情节提要并且从当前场景到目标场景有转场,您只需回复 calloutAccessoryControlTapped。例如:

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

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

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

    【讨论】:

    • 谢谢 Rob,我是否需要使用情节提要才能拥有 Segue?
    • 请好好研究一下。一如既往 - 你摇滚
    • 那么我如何访问新视图上的字段?我正在尝试在我的应用中完成同样的事情。
    猜你喜欢
    • 2014-06-23
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多