1

My first view controller, AllAthletes, is a uitableview of all my core data entities. It displays the entity "athlete" and it's properties in a subtitle style table cell such as first name, etc.. When you click on a view cell, I want this view controller to pass the information of the entity that was selected but i'm unsure on how to pass this information (managedobject, entity, properties, etc of indexpath) along to the detail view controller. Could someone please point me in the right direction?

allathletes.m

-(void)viewWillAppear:(BOOL)animated{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    _managedObjectContext = [appDelegate managedObjectContext];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *athlete = [NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:_managedObjectContext];
    [request setEntity:athlete];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"first" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];

    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil){
        //handle error
    }
    [self setAthleteArray:mutableFetchResults];
    [self.tableView reloadData];
}



 //this is what I have so far
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
     if ([segue.identifier isEqualToString:@"setAthlete"]) {
         NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
         AthleteDetail *destViewController = segue.destinationViewController;
     }
 }

//i was also thinking something like this?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController* detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    NSMutableString *object = thisArray[indexPath.row];
    detailViewController.passedData = object;
    [self.navigationController pushViewController:detailViewController animated:YES];
}

The second controller that it pushes you to is AthleteDetail. Thank you for your time.

Josue Espinosa
  • 129
  • 3
  • 10
  • Well, you need to use `performSegueWithIdentifier:sender:`. You are pushing manually now. – Desdenova Aug 01 '13 at 13:54
  • It's not clear what your specific problem is. How to do this is explained in the "Navigating a Data Hierarchy with Table Views" section of the "Table View Programming Guide for iOS". Read that, and if you still don't understand, ask a more specific question. – rdelmar Aug 01 '13 at 15:00
  • @rdelmar is that better? – Josue Espinosa Aug 01 '13 at 15:19
  • Not really. What exactly are you trying to pass? – rdelmar Aug 01 '13 at 15:29
  • @rdelmar I need the controller thats being pushed to know everything about the entity that was selected in the table. So if I tap a table cell with the name Josue (instance of athlete entity), the detail view controller needs to know all the attributes for it. – Josue Espinosa Aug 01 '13 at 16:13
  • Then you probably need to pass the indexPath, so the detail controller can get the same entity out of your database, but that depend on how you're populating your rows of the table. – rdelmar Aug 01 '13 at 21:02

1 Answers1

3

You are mixing two different concepts. Instead of

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController* detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    NSMutableString *object = thisArray[indexPath.row];
    detailViewController.passedData = object;
    [self.navigationController pushViewController:detailViewController animated:YES];
}

You should do something like

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:YOUR_SEGUE_IDENTIFIER sender:self];        
}

and then:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
     if ([segue.identifier isEqualToString:YOUR_SEGUE_IDENTIFIER]) {
         NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
         AthleteDetail *destViewController = segue.destinationViewController;
         // set whatever you want here in your destination view controller
         destViewController.passedData = object;
     }
 }

And make sure that your Segue is a Push Segue.

Bruno Koga
  • 3,864
  • 2
  • 34
  • 45