NSObjectController - setAutomaticallyPreparesContent:
我有一个小型核心数据应用程序。我有一个 NSObjectController 的子类,充当视图和模型之间的绑定,以及视图上的 NSTextField 供用户输入。
当窗口打开时,文本字段是可编辑的,因为我将绑定设置为我的子类 NSObjectController,将控制器键设置为“内容”,将模型键路径设置为我的实体的属性。
如果我输入一些文本,保存文件并重新打开它,则 NSTextField 中的文本不存在。
为了进行测试,我有一个按钮连接到控制器的 add: 选择器,当您按下该按钮时,一切正常 - 您可以在 NSTextField 中输入文本,您可以保存文档,您可以再次打开它。
笔尖加载时如何准备对象?
在我的 NSObjectController 子类中的 init 方法中,我有:
[self setAutomaticallyPreparesContent:YES];
然后在 MyDocument:windowControllerDidLoadNib 中(oc 是 IB 中子类对象控制器的 IBOutlet):
[oc fetchWithRequest:nil merge:NO error:&error];
但它不起作用。我需要创建内容以便用户可以开始输入。
谢谢
I have a small core data app. I have a subclass of an NSObjectController acting as the binding between the view and the model and a NSTextField on the view for the user to type into.
When the window opens the text field is editable because I have the bindings set to my subclassed NSObjectController, controller key to "content" and the Model Key Path to an attribute of my Entity.
If I type in some text, save the file and reopen it the text in the NSTextField isn't there.
For testing, I have a button connected to the add: selector of the controller and when you press the button everything works fine - you can enter text into a NSTextField, you can save the document, you can open it again.
How do I prepare the object when the nib loads?
In my init method in my subclass of the NSObjectController I have:
[self setAutomaticallyPreparesContent:YES];
and then I have in MyDocument:windowControllerDidLoadNib (oc is the IBOutlet to the subclassed objectcontroller in IB):
[oc fetchWithRequest:nil merge:NO error:&error];
but it didn't work. I need to create the content so the user can get started typing.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这是我的贡献。我很高兴能够回答一些问题!我在文档中的“NSPersistentDocument核心数据教程”中找到了这一点。
请记住,我的问题是当创建文档时我想创建一个 NSManagedObject。这样用户就不必点击“添加”按钮。我不希望文档变脏(直到用户输入某些内容),并且在打开保存的文件时我不想替换内容。仅当创建新文档时才需要执行此操作。
NSDocument 提供了一个方法:
来完成这个任务。
1) 获取 ManagedObjectContext,
2) 暂时关闭撤消功能。这可以防止文档变脏并阻止用户撤消实体的创建和插入。
3) 使用 insertNewObejctForEntityForName:inManagedObjectContext
4) 安装更改
5) 返回撤消功能
这是代码:
OK, here's my contribution. I'm happy to be able to answer something! I found this in the "NSPersistentDocument Core Data Tutorial" in the documentation.
Remember, my problem was when the document is created I want to create an NSManagedObject. That way the user doesn't have to hit the "add" button. I don't want the document to be dirty (until the user types something) and I don't want to replace the content if I'm opening a saved file. This needs to happen only when a new document is created.
NSDocument provides a method:
to accomplish this.
1) grab the managedObjectContext,
2) turn off undo for the moment. This prevents the doc from being dirty and preventing the user from undoing the creation and insertion of the Entity.
3) use insertNewObejctForEntityForName:inManagedObjectContext
4) install the changes
5) turn back on undo
Here's the code: