-2

我有一个列出运动员的表格视图。when an athlete is selected, I wish to have the detail view controller (the controller that is pushed onto the stack) to know all the attributes about the athlete. 他/她的姓名、生日、电话号码等。但我不确定如何传递这些信息。

运动员.h

-(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:@"last" 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];
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSString *segueIdentifier = [segue identifier];
    if ([segueIdentifier isEqualToString:@"setAthlete"])
    {
        UINavigationController *navController = (UINavigationController *)[segue destinationViewController];
        AllAthletes *athleteList = (AllAthletes *)[[navController viewControllers] lastObject];
        //the line below gets an error :(
        AthleteDetail.managedObjectContext = self.managedObjectContext;
    }
}
4

2 回答 2

0

我想你会想要使用代表。这是一个关于如何做到这一点的很棒的教程:链接

于 2013-08-05T00:07:30.300 回答
0

在推送详细视图控制器之前,在其上设置要显示的数据的属性,例如:

myDetailViewController.myModel = selectedModel;

在详细视图中,您可以使用 viewWillAppear 中的数据设置视图。

于 2013-08-04T23:31:29.793 回答