我可以只使用Interface Builder和Core Data向选定的父对象添加NSManagedObject? [英] Can I add an NSManagedObject to the selected parent object just using Interface Builder and Core Data?

查看:139
本文介绍了我可以只使用Interface Builder和Core Data向选定的父对象添加NSManagedObject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Core Data,目前只需使用Interface Builder和XCode数据模型(按照Aaron Hillegass Cocoa书籍示例)。

I'm learning Core Data and at the moment just using Interface Builder and the XCode data model (following the Aaron Hillegass Cocoa book examples).

MVC应用程序和核心数据在界面构建器中设置和工作(还没有手写代码)。我已经设置了各种数组控制器,视图和按钮添加和删除对象和编辑数据,并确定对象之间的关系。

I have an MVC application and core data set up and working within interface builder (no hand-written code yet). I've set up various arraycontrollers, views and buttons to add and remove objects and edit data and determine relationships between objects.

但是我的对象模型是严格分级父子(和孙子)和每个关系是一对多。因此,当我添加一个子(或孙)时,我想自动将当前在主tableView中选择的父对象作为新子对象的父对象。

However my object model is strictly hierarchical parent-child (and grandchildren) and each relationship is one-to-many. So when I add a child (or grandchild) I want to automatically assign the parent object currently selected in the main tableView as the parent of the new child object.

可能在界面生成器中,或者这是我必须开始编码我自己的方法的点?

Is this possible in interface builder or is this the point where I have to start coding my own methods?

下一步我应该采取或如何图形的任何指针它自己很感激。我只是有点卡住,找不到相关的教程或参考。

Any pointers on the next step I should be looking to take or how to figure it out myself much appreciated. I'm just a bit stuck and can't find a relevant tutorial or reference.

如果我描述我的文档窗口也许会有帮助。它有两个带有添加/删除按钮的tableView。右侧的tableView显示在左侧tableView中选择的父对象的子对象的NSSet。我想要右侧的添加按钮添加一个新的对象,并自动使其成为所选父对象的子对象,因此它出现在右侧的tableView中。

Perhaps it will help if I describe my document window. It has two tableViews with add/remove buttons. The tableView on the right displays an NSSet of the child objects of the parent objected selected in the tableView on left. I want the add button on the right to add a new object and automatically make it a child of the selected parent object so it appears in the tableView on the right.

现在,我可以在arrayController上看到子对象的唯一方法是add:和remove:但是当然不会将新对象连接到当前选择的父级。我需要添加我自己的(IBAction)方法到父对象,即addChildObject:?或者有一个内置的方法来基于父子关系在CoreData中自动添加子项?

At the moment the only methods I can see on the arrayController for the the child objects are add: and remove: but of course they don't connect the new object to the currently selected parent. Do I need to add my own (IBAction) method to the parent object i.e. addChildObject:? Or is there a built in way to add the child automatically within CoreData based on the parent-child relationship?

推荐答案

问题解决。我发现一个教程(尽管有点过时)描述了我想要做什么:

Problem solved. I found a tutorial (albeit a bit dated) describing exactly what I am trying to do:

https://developer.apple.com/cocoa/coredatatutorial/index.html

并仔细地按照每一步之后,我意识到我做了一切正确,我刚刚错过了一个绑定:我只设置子数组控制器的内容集,而不是其管理对象上下文(文件的所有者)。这是我添加按钮没有添加一个新的子对象的唯一原因!平凡的错误,但是当你不确信你问你是整个方法,而不是寻找一个简单的错误。对不起,任何人谁不得不阅读这个,但也许链接到教程将有助于某人。

And after studiously following every step carefully I realised I had done everything right I had just missed one binding: I had only set the Content Set of the child array controller and not its Managed Object Context (to File's Owner). That is the only reason my add button was not adding a new child object! Trivial mistake, but when you are not confident you question you're whole approach rather than looking for a simple error. Sorry for anyone who had to read this but maybe the link to the tutorial will help someone.

这篇关于我可以只使用Interface Builder和Core Data向选定的父对象添加NSManagedObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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