Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKAnnotationView Push to View Controller when DetailDesclosure Button is Clicked

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

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!

like image 699
Brandon Avatar asked Feb 11 '13 04:02

Brandon


1 Answers

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

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.

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

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.


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
}
like image 169
Rob Avatar answered Oct 26 '22 13:10

Rob