我已经搜索了很多这个问题的答案,并通过这样做想出了 zilch。
尝试连接主 VC 中的父实体和子实体并通过 prepareForSegue 将详细信息传递给详细信息 VC 无法正常工作:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"showDetails"]){
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController * detailsVC = segue.destinationViewController;
Parent * selectParent= (Parent *) [[self fetchedResultsController] objectAtIndexPath:indexPath];
Children * children = (Children *) [[self fetchedResultsController] objectAtIndexPath:indexPath];
detailsVC.selectedParent = selectedParent;
detailsVC.showChildren = children;
}
}
在 prepareForSegue 和 DetailVC 的其他几个变体中,由于 fetchedResults 中的引用直接指向父母,它无法正常工作并引发错误,尽管对子集有任何子引用。
阅读 NSFetchedResultsController 文档时我发现了这个:
您使用获取的结果控制器来有效地管理从核心数据获取请求返回的结果,以便为 UITableView 对象提供数据。
虽然可以通过多种方式使用表视图,但获取结果控制器主要用于帮助您使用主列表视图。
这实际上表明fetchedResults 用于在表格视图中显示数据,并假定任何其他视图都无法正确使用fetchedResults。
为了克服这个问题,我在 Detail VC 的未使用空间中使用了一个非常小的表视图控制器,并将其 alpha 设置为 0.0。
显示子类详细信息的方法在下面的方法中执行:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath:indexPath];
Children * childDetails = [self.fetchedResultsController objectAtIndexPath:indexPath];
self.childNameLabel.text = childDetails.childFirstName;
return cell;
}
这很好地解决了我的问题。我知道这是作弊,但它奏效了。我希望我的发现对处于相同情况的人有所帮助。
如果有更好的方法,请回答,我很乐意给它打勾。