具有多个 viewController 的核心数据
我在让核心数据在我的应用程序中工作时遇到很多困难。我对表视图控制器中的核心数据非常满意,甚至对单视图应用程序中的核心数据也非常满意。然而,我无法让我的程序在多个视图控制器中正常运行。
我已经阅读了 Zarra 的核心数据手册,并购买了 iOS 的专业核心数据,并浏览了列出的项目,但它们中的每一个都在 TableView 控制器中使用。 iOS 专业核心数据中的形状应用程序除外。
有谁知道有任何示例(代码或教程)可以演示如何使用多个视图控制器和核心数据编写程序?
我想做的是在第一个(而不是表视图单元格)上有按钮,这些按钮将继续到下一个视图控制器。在第二个视图控制器上,我希望该信息填充来自第一个实体集合的信息,
因此到目前为止,第一个实体是这样的:
House
- houseName(属性)
- 占用者(关系)
Person
- personName(属性)
- 家(关系)
占用者 <-->>家庭(一对多)
{
...
int i = //house selected on previous view controller;
NSManagedObject *people = [[self sortOccupants] objectAtIndex:i];
textField01.text = [NSString stringWithFormat:@"%@",[[people valueForKey:@"personName"]description]];
}
排序占用者看起来像这样:
-(NSArray *)sortOccupants
{
NSSortDescriptor *sortPeopleInHouse = [[NSSortDescriptor alloc] initWithKey:@"personName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortPeopleInHouse, nil];
return [[(NSSet *)[house valueForKey:@"occupants"] allObjects] sortedArrayUsingDescriptors:sortDescriptors];
}
任何想法都会很棒,但如果你能指出我的示例代码展示这一点我将非常感激。
谢谢,
I'm having a lot of difficulty getting core data to work in my application. I'm perfectly comfortable with core data in a table view controller and even with core data in a single view application. I'm unable however to get my program to function properly in with multiple view controllers.
I've read through Zarra's core data book and bought pro core data for iOS and have gone through the projects listed but every one of them is used in a TableView controller. with the exception of the shapes application in pro core data for iOS.
Does anyone know of any examples (code or tutorials) that would demonstrate how to do a program with multiple view controllers and core data?
What I would like to do is have buttons on the first (instead of tableview cells) that will segue to the the next viewController. On the second view controller I would like that information populated with information from the set of the first entity,
so I have something like this so far where the first entity is:
House
- houseName (attribute)
- occupants (relationship)
Person
- personName (attribute)
- household (relationship)
occupants <-->>household (one to many )
{
...
int i = //house selected on previous view controller;
NSManagedObject *people = [[self sortOccupants] objectAtIndex:i];
textField01.text = [NSString stringWithFormat:@"%@",[[people valueForKey:@"personName"]description]];
}
the sort occupants looks like this:
-(NSArray *)sortOccupants
{
NSSortDescriptor *sortPeopleInHouse = [[NSSortDescriptor alloc] initWithKey:@"personName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortPeopleInHouse, nil];
return [[(NSSet *)[house valueForKey:@"occupants"] allObjects] sortedArrayUsingDescriptors:sortDescriptors];
}
Any Ideas would be great but if you can point me to sample code that would show this I would be most appreciative.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我处理此问题的方法是为您的实体生成 NSManagedObject 子类(使其更具可读性和类型安全性)。
然后,我将在第二个视图控制器中创建一个新的 init 方法。 initWithHouse:(House *)house 或其他东西:
希望有帮助。
The way I would approach this is to generate NSManagedObject subclasses for your entities (makes it much more readable and type-safe).
Then, I would create a new init method in the second view controller. initWithHouse:(House *)house or something:
Hope that helps.
也许关键是每个视图控制器都应该
在其标题中。
在推送视图控制器之前,您需要为其提供托管对象上下文。在这个新的视图控制器中,您可以使用典型的方法来搜索和保存上下文。
您从主视图控制器开始,它实际上是一个人员列表。您选择一个人并启动一个人视图控制器。您应该将托管对象上下文和 Person 的实例传递给此 person 视图控制器。
Maybe the key is that each view controller should have
in its header.
Before you push a view controller you give it a managed object context. In this new view controller you can have your typical methods for searching and saving the context.
You start with a home view controller, which is really a list of people. You select a person and launch a person view controller. You should pass a managed object context and an instance of Person to this person view controller.