如何在视图控制器之间传递对象 [英] how to pass objects between view controllers

查看:138
本文介绍了如何在视图控制器之间传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表视图列出运动员。当选择运动员时,我希望具有详细视图控制器(被推到堆栈上的控制器)以知道关于运动员的所有属性。他/她的姓名,生日,电话号码等。但是我不确定如何传递这些信息。

I have a table view that lists athletes. 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. his/her name, birthday, phone number, etc. But im unsure on how to pass this information.

allathletes.h

allathletes.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;
    }
}


推荐答案

我想你会想使用委托。这是一个很好的教程, href =https://www.youtube.com/watch?v=kFLK_1Shwe8 =nofollow>链接

I think you're going to want to use delegates. Here is a great tutorial on how to do that: Link

这篇关于如何在视图控制器之间传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆