如何在 NSUserDefaults 中绑定 NSDictionary 中的对象?
如何将 NSDictionary 对象绑定到 Interface Builder 中的多个文本字段?
我想将每个对象绑定到字典中的特定项目。例如,第一个文本字段应绑定到 Actor:
How do I bind NSDictionary objects to several text fields in Interface Builder?
I would like to bind each object to a specific item in the dictionary. For example, the first text field should be bound to Actor:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在 Interface Builder 中添加 NSObjectController。在属性检查器选项卡中,将标准模式“Class”和类名称保留在“NSMutableDictionary”上,但打开“准备内容”。在绑定检查器中,将“内容对象”绑定到“共享用户默认控制器”,将控制器键“值”和“模型键路径”绑定到用户默认值中字典的键。打开“将内容作为复合值处理”。
现在您可以绑定文本字段(或复选框等)。值到 ObjectController,使用控制器键“选择”(!)和字典中的键(作为模型键路径)。
希望这有帮助。我在苹果的文档中找不到答案,在网上也找不到答案,很幸运我自己通过尝试不同的控制器对象找到了答案......
You need to add a NSObjectController in Interface Builder. In the attributes inspector tab leave the standard mode "Class" and class name on "NSMutableDictionary", but switch "Prepares Content" on. In the bindings inspector bind "Content Object" to "Shared User Defaults Controller", controller key "values" and "Model Key Path" to your dictionary's key in User Defaults. Switch "Handles Content As Compound Value" on.
Now you can bind your text field (or check boxes etc). values to the ObjectController, with controller key "selection" (!) and the keys in your dictionary (as model key path).
Hope this helps. I couldn't find the answer in Apple's documentation and also not in the net, was lucky to find it out myself by trying different controller objects...
首先:@keeluu 的答案应该被标记为正确。
以编程方式执行
如果您使用代码而不是通过 IB 执行此操作,则需要注意一个“陷阱”。它看起来像这样:
如果执行上述操作,您会发现当您将 UI 元素绑定到
someDictionary
中的属性时,这些 UI 元素将正确显示用户默认值,但是当您更改它们(例如,单击绑定的复选框)这些值将不会在用户默认值中更新;旧的价值观依然存在。原因
绑定必须是
NSContentObjectBinding
而不是NSContentBinding
。 NSObjectController 类公开了一个名为contentObject
的绑定。当你绑定到这一点时,一切都会正常进行。这是一个很容易犯的错误,因为 Xcode 的代码完成会自动使用
NSContentBinding
并且它是最常用的绑定名称之一。这只是给我带来了几个小时的麻烦,所以希望我可以节省其他人一些时间。First: @keeluu's answer should be marked correct.
Doing it Programmatically
If you're doing this in code rather than through IB, there's a "gotcha" to look out for. It looks like this:
If you do the above, you'll find that when you bind your UI elements to properties in
someDictionary
, those UI elements will correctly display the values that are IN user defaults, but when you change them (by say, clicking a bound checkbox) those values will NOT be updated in user defaults; the old values stick around.The Reason
The binding must be to
NSContentObjectBinding
instead ofNSContentBinding
. The NSObjectController class exposes a binding namedcontentObject
. When you bind to that, everything works properly.This is an easy mistake to make because Xcode's code completion automatically goes for
NSContentBinding
and it's one of the most frequently used binding names anyway. This just cost me hours of trouble, so hopefully I saved someone else some time.尝试将
NSDictionaryController
添加到您的 .xib 文件中,然后将其控制器内容绑定到用户默认值中的字典。然后,您可以将文本字段值绑定到字典控制器(arrangedObjects 键)。我从未尝试过,但我认为它一定有效。
希望,有帮助。
Try to add
NSDictionaryController
to you .xib file, then bind its Controller Content to your dictionary in User Defaults. Then you can bind text field values to Dictionary Controller (arrangedObjects key).I never tried that but I think it must work.
Hope, it helps.