相关疑难解决方法(0)

将ManagedObjectContext传递给第二个视图

我正在写我的第一个iPhone/Cocoa应用程序.它在导航视图中有两个表视图.当您触摸第一个表视图中的行时,您将进入第二个表视图.我希望第二个视图显示与您在第一个视图中触摸的行相关的CoreData实体的记录.

我在第一个表视图中显示了CoreData数据.您可以触摸一行并转到第二个表格视图.我能够将所选对象的信息从第一个视图传递到第二个视图.但我无法获得第二个视图来进行自己的CoreData获取.对于我的生活,我无法将managedObjectContext对象传递给第二个视图控制器.我不想在第一个视图中执行查找并传递字典,因为我希望能够使用搜索字段来优化第二个视图中的结果,以及从那里向CoreData数据插入新条目.

这是从第一个视图转换到第二个视图的函数.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here -- for example, create and push another view controller.
    NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];

    secondViewController.tName = [[selectedObject valueForKey:@"name"] description];
    secondViewController.managedObjectContext = [self managedObjectContext];

    [self.navigationController pushViewController:secondViewController animated:YES];
    [secondViewController release];
}
Run Code Online (Sandbox Code Playgroud)

这是SecondViewController中崩溃的函数:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = tName;

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) { // <-- crashes here
        // Handle the error...
    }
}

- (NSFetchedResultsController …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch core-data objective-c

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

cocoa-touch ×1

core-data ×1

iphone ×1

objective-c ×1