xcode 单击 DetailDesclosure 按钮时,MKAnnotationView 推送到视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14805954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MKAnnotationView Push to View Controller when DetailDesclosure Button is Clicked
提问by Brandon
I would like to switch views when a DetailDisclosure is clicked on a map I am displaying. My current code is as below:
我想在我显示的地图上单击 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];
}
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:
我可以用这个推送到视图控制器,但我还没有想出如何强制它从用于生成地图的 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?
I know I'm a bit off target. Maybe just a kick in the right direction might help. Thank you!
我知道我有点偏离目标。也许只是朝着正确的方向踢一脚可能会有所帮助。谢谢!
回答by Rob
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:
如果您正在使用情节提要并且有从当前场景到目标场景的转场,您只需响应calloutAccessoryControlTapped
. 例如:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[self performSegueWithIdentifier:@"Details" sender:view];
}
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.
显然,view
如果您想为不同的注释类型调用不同的 segue ,您可以检查与 that 关联的注释类型,等等。
And, of course, if you want to pass any information to that next scene in prepareForSegue
like usual.
而且,当然,如果您想像prepareForSegue
往常一样将任何信息传递给下一个场景。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Details"])
{
MKAnnotationView *annotationView = sender;
[segue.destinationViewController setAnnotation:annotationView.annotation];
}
}
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.
如您所见,我将annotation
对象传递到下一个视图。如果您的 JSON 结构中有与每个注释关联的其他字段,一个简单的解决方案是向与您要跟踪的每个字段关联的自定义视图添加属性。只需转到您的注释自定义类的 .h 并添加您需要的属性。然后,当您创建自定义注释(要添加到地图中)时,也只需设置这些属性。然后,当您将此注释传递给下一个视图控制器时,您所需的所有属性都将在那里可用。
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.:
显然,如果您使用 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
}