简体   繁体   English

将核心数据对象传递给didSelectRowAtIndexPath上的子视图

[英]Passing a core data object to a sub view on didSelectRowAtIndexPath

I have a core data setup that has 2 database entities. 我有一个核心数据设置,有2个数据库实体。 For the sake of names I'll call them Primary and Secondary. 为了名字我会称他们为小学和中学。 Secondary only belong to one Primary (relationship is setup). 辅助仅属于一个主要(设置关系)。 In my main view that lists the Primary objects in a table I retrieved them and put them in an PriObject Class which stores it's properties (including the managed object ID). 在我的主视图中,列出了表中的Primary对象,我检索了它们并将它们放在一个PriObject类中,该类存储它的属性(包括托管对象ID)。 The PriObject is then added to a mutable array (priArray), which is then used to fill the table with the data. 然后将PriObject添加到可变数组(priArray)中,然后使用该数组将数据填充到表中。 All works ok so far. 到目前为止一切正常。 When I then click on the row I can log the PriObject.moID. 当我单击该行时,我可以记录PriObject.moID。

I can't figure out how to lookup that object in the database so I can then add Secondary objects to it. 我无法弄清楚如何在数据库中查找该对象,以便我可以向它添加辅助对象。 I can't do it by name because some Primary might have the same name. 我无法通过名称来执行此操作,因为某些主节点可能具有相同的名称。

I need to work out how to get an object back from either the URI or the ID. 我需要弄清楚如何从URI或ID中获取对象。 I have the ID so I can generate the URI if I need to. 我有ID,所以如果需要我可以生成URI。

Can't get my head around it at all and any examples I have found while looking don't cover what I need. 根本无法理解它,我看到的任何例子都没有涵盖我需要的东西。 What options are there? 有什么选择?

EDIT: I am currently getting all my objects with the following. 编辑:我目前正在使用以下内容获取所有对象。

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Primary"
                                              inManagedObjectContext:context];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];

NSError *error;
NSArray *objects = [context executeFetchRequest:request
                                          error:&error];


if ([objects count] == 0) {
    NSLog(@"Nothing found");
} else {
    NSLog(@"Something found");
}

How can I change this for just the one using: 如何使用以下内容更改此选项:

ObjectWithID:

Call the objectWithID: method on your NSManagedObjectContext instance to retrieve the instance. NSManagedObjectContext实例上调用objectWithID:方法以检索实例。

As an aside, it seems like you are making things harder on yourself with this PriObject class, it seems to be a wrapper around your NSManagedObject instances, is that right? PriObject一下,看起来你正在使用这个PriObject类让自己变得更难,它似乎是你的NSManagedObject实例的包装,是吗? I'd just use NSManagedObject subclasses directly, personally. 我个人直接使用NSManagedObject子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 核心数据和didSelectRowAtIndexPath - Core Data and didSelectRowAtIndexPath 更新didSelectRowAtIndexPath上的核心数据 - Update Core Data on didSelectRowAtIndexPath 将数据从didSelectRowAtIndexPath传递到另一个视图控制器ios7 - passing data from didSelectRowAtIndexPath to another view controller ios7 didSelectRowAtIndexPath没有将URL传递给下一个视图控制器? - didSelectRowAtIndexPath not passing url to the next view controller? 使用核心数据使用didSelectRowAtIndexPath编辑TableView单元格 - Editing a TableView Cell with didSelectRowAtIndexPath Using Core Data 将核心数据对象传递给新的Table View Controller,但使用对象的关系在新的View Controller中显示该数据 - Passing a Core Data Object to a new Table View Controller, but Using the Object's Relationship to Display That Data in the new View Controller didSelectRowAtIndexPath 没有改变我传递给前一个视图的属性 - didSelectRowAtIndexPath is not changing the property I am passing to a previous View 传递核心数据对象而不保存 - passing core data object without saving 将核心数据对象从tableViewController传递到TabBarController - Passing Core Data object from tableViewController to TabBarController Swift:didSelectRowAtIndexPath时如何更新核心数据中的数据? - Swift: How to update data in Core Data when didSelectRowAtIndexPath?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM