将对象添加到 NSDictionaryController
我有一个绑定到 NSDictionaryController 的 NSTableView。我已经通过界面生成器设置了它和内容字典。我的问题是,如何将对象添加到字典中,以便控制器自动看到它并将其显示在表中。
我在有关 NSArrayController 的示例中看到,您通过控制器而不是实际数组添加新对象,以便控制器看到更改。但是,我没有看到使用字典控制器执行此操作的类似方法...
感谢您的帮助。
I have an NSTableView which is bound to an NSDictionaryController. I have set this and the content dictionary through the interface builder. The question I have is, how do I add objects to the dictionary in a manner that the controller will see it automatically and display it in the table.
I've seen in examples about NSArrayController that you add the new objects through the controller rather than the actual array so the controller sees the change. However, I don't see a similar way of doing this with the dictionarycontroller...
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
出色的!针对 Swift3 修改:
Excellent! modified for Swift3:
杰森的答案可以简化:
Jason's answer can be simplified:
好吧,我最终弄清楚了这一点。我认为由于数组控制器镜像了数组的使用,所以字典控制器也会做同样的事情,但事实并非如此。这个过程有点不同......
所以如果你有一个字典控制器,你想添加值,以便它可以更新任何绑定到它的东西,你使用以下过程:
这将有效,更新字典控制器并添加内容字典的值。在这种情况下您不能使用 setValue:ForKey 因为这不是 NSDictionaryControllerKeyValuePair 协议中实现的方法。
我希望这可以帮助其他使用这些控制器的人!
Well I ended up figuring this out. I figured since the arraycontroller mirrored the use of an array that a dictionarycontroller would do the same but it is not so. The process is a little bit different...
So if you have a dictionarycontroller that you would like to add values to so it can update anything binding to it you use the following process:
This will in effect, update both the dictionarycontroller and add the value to the content dictionary. You cannot use setValue:ForKey in this case because this is not a method implemented in the NSDictionaryControllerKeyValuePair Protocol.
I hope this can help others using these controllers!
jasons 的答案对我不起作用,因为在我的实现中,它在 [newObject setValue:@"value"] 行上遇到了错误,说发现了多个结果不匹配的方法,并且我找不到任何其他方法可以按预期添加新条目。
经过大量搜索和尝试,我想出了这个解决方案:
每次 dictController 需要更新时只需更新绑定,如下所示:
因此在我的代码中,我将项目添加到我的字典中,并且在显示包含字典内容的表格之前,我重置了绑定。这可能是一个肮脏的解决方案,但它对我有用。
jasons answer didn't work for me because in my implementation it ran into an error on the line [newObject setValue:@"value"], saying that multiple methods were found with mismatched results and i couldn't find any other method that would add the new entry as intended.
after a lot of searching and trying i came up with this solution:
just renew the binding everytime the dictController needs to be updated, like this:
so throughout my code i add items to my dictionary, and just before showing the table with the contents of my dictionary i reset the binding. this is probably a dirty solution, but it worked for me.