在1个非表视图控制器中显示多个核心数据实体对象 [英] Display multiple core data entities objects in 1 Non-Table View Controller

查看:61
本文介绍了在1个非表视图控制器中显示多个核心数据实体对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定标题的字眼,如果有人问过我,我也表示歉意。我还没有找到答案。

I am unsure of how to word the Title and I apologise in advance if it's been asked before. I haven't found the answer.

是否可以在单个视图中显示两个(或更多)核心数据内容?例如,如果您有一个名为父级的实体,而又有一个名为子级的实体,则父级与子级具有一对多关系,我可以在视图控制器的详细信息中显示以下内容:

Is it possible to display two (or more) Core Data contents in a single view? As an example, if you have an entity called Parents and one called Children, Parents has a ONE-MANY relationship with Children, can I display in a detail View Controller the following:

父母的名字:爸爸妈妈

孩子:小孩1小孩2小孩3

Children: Child 1 Child 2 Child 3

目前我只能除非我在VC中添加了一个表格视图,否则似乎只能在一个视图中显示一个或另一个视图。但是这种解决方案并非在每种情况下都切实可行。

At present I can only seem to display one or the other in a single view unless I add embed a Table View into the VC, but this solution isn't really viable in every scenario.

事件流很简单:

Table View Controller在Parents CD实体中显示父母的姓名。点击一行,然后转到详细信息VC。在这里,我要显示所选父母的姓名以及已保存到其中的所有子女的姓名。

Table View Controller displaying the Name of parents in the Parents CD entity. Tap a row and then go to the details VC. From there I want to display the names of the parents selected and any children that has been saved to them.

有人可以回答这个简单的问题吗?

Can anyone answer this likely simple issue?

推荐答案

您的获取结果控制器配置为(大概)配置为获取父对象。如您所知,您不能要求它提供Child对象的详细信息。但是Parent对象应该具有引用它们的属性。如果该属性被命名为 children,则可以按以下方式访问任何给定父级的子级:

Your fetched results controller is configured (presumably) to fetch Parent objects. As you have found, you cannot ask it to provide details of the Child objects. But Parent objects should have a property which references them. If that property is named "children", then you can access the children of any given parent as follows:

NSSet *currentChildren = parent.children;

NSSet *currentChildren = [parent valueForKey:@"children"];

因此,在您的 prepareForSegue 方法中,如果您设置 detailsVC.selectedParent = selectedParent; ,然后在您的detailsVC中,您可以作为 self.selectedParent.children 访问子级。这将是一组无序的Child对象,但是您可以使用 for(in)枚举它们。例如:

So, in your prepareForSegue method, if you set detailsVC.selectedParent = selectedParent; then within your detailsVC, you can access the children as self.selectedParent.children. That will be an unordered set of Child objects, but you can enumerate them all using for ( in ). For example:

for (Child *child in self.selectedParent.children) {
    NSLog(@"Child name is %@", child.childFirstName);
}

应记录所选父母的所有孩子的名字。

should log the first names of all the children of the selected parent.

这篇关于在1个非表视图控制器中显示多个核心数据实体对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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