在视图控制器segue之间传递数据

时间:2013-08-01 13:44:33

标签: ios objective-c cocoa-touch

我的第一个视图控制器AllAthletes是我所有核心数据实体的合适视图。它显示实体“运动员”及其在字幕样式表格单元格中的属性,例如名字等。当您单击视图单元格时,我希望此视图控制器传递所选实体的信息但是我我不确定如何将此信息(indexpath的managedobject,entity,properties等)传递给详细视图控制器。有人可以指出我正确的方向吗?

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

它推动你的第二个控制器是AthleteDetail。谢谢你的时间。

1 个答案:

答案 0 :(得分:3)

您正在混合两个不同的概念。而不是

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

您应该执行类似

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

然后:

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

并确保你的Segue是推送Segue。