gpt4 book ai didi

iphone - 单击 DetailDesclosure 按钮时 MKAnnotationView 推送到 View Controller

转载 作者:可可西里 更新时间:2023-11-01 03:08:17 25 4
gpt4 key购买 nike

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

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

我可以用它推送到 View Controller ,但我还没有想出如何强制它从最初用于生成 map 的 JSON 数组中提取详细信息。我正在拉取这样的数据来生成 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?

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

最佳答案

如果您正在使用 Storyboard并且有从当前场景到目标场景的转场,您可以只响应 calloutAccessoryControlTapped。例如:

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

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

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

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

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


显然,如果您使用的是 NIB,只需执行 NIB 等价物即可实例化下一个 View Controller ,设置您想要的任何属性,然后推送或以模态方式呈现它,例如:

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

关于iphone - 单击 DetailDesclosure 按钮时 MKAnnotationView 推送到 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14805954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com