Question

I have a map view which is populated with objects called Venues using Core Data. I turn the data into map annotations through a fetchedResultsController, like this:

-(void) createAnnotations { 
    _annotations=[[NSMutableArray alloc] init];

    for(Venue *selectedVenue in [self.fetchedResultsController fetchedObjects]) {

        currentVenue = NULL;

        double clatD = [selectedVenue.clat doubleValue];
        double clongD = [selectedVenue.clong doubleValue];

        CLLocationCoordinate2D selectedCoord;
        selectedCoord.latitude = clatD;
        selectedCoord.longitude = clongD;

        MyAnnotation* annot=[[MyAnnotation alloc] init];

        annot.coordinate=selectedCoord;
        annot.title=selectedVenue.title;
        annot.subtitle=selectedVenue.address;

        [_annotations addObject:annot];
    }

I then plot the annotations on the map using:

[mapView addAnnotations:_annotations];

There may be a better way of doing thing, but hey, it works.

Anyway, the problem. I have it set so that when the user presses the annotation it segues to a detail view which is intended to have more info about that particular Venue object. This is where the information gets passed, and it's where I'm stuck:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showVenueDetailFromMap"]) {
        DisplayDetailViewController *ddvc = (DisplayDetailViewController *) [segue destinationViewController];

// This is where I want to pass the chosen object to the next view. But how?        
     Venue *selectedVenue = (Venue *) [self.fetchedResultsController objectAtIndexPath:???];
     ddvc.currentVenue = selectedVenue;
    }
}

I'm thinking it may have something to do with this next piece of code. As far as I can tell this is the only place where the app knows which Venue has been selected. But all it knows is the contents of the annotation (ie. title, subtitle, coordinates), so how do I link it back to the original Venue object from the fetchedResultsController?

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

    NSLog(@"%@",view.annotation.title);
    NSLog(@"%@",view.annotation.subtitle);
}

A note on the above. I have this setup working absolutely fine from a table view. I just reference the selected row like this:

 NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
 Venue *selectedVenue = (Venue *) [self.fetchedResultsController objectAtIndexPath:indexPath];

I don't know how to do the same thing using my annotation-based map setup rather than my nice table setup full of rows and sections.

I hope this makes sense. I appreciate it's a lot of writing for quite a simple question. Basically what I am asking is "how do I reference an object from the fetchedResultsController when using a map view rather than a table view?"

Any thoughts would be much appreciated!

Was it helpful?

Solution

Modify your MyAnnotation class to hold an index path property. Set this as you create the annotations by requesting indexPathForObject:selectedVenue from the FRC.

When the annotation is selected you can now get the index path and store it as the currentVenue (or perhaps pass it as the sender of the segue).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top