Implementing parent->child drill down in Cocoa with Core Data bindings that span multiple entities

随声附和 提交于 2019-12-09 04:17:11

问题


I'm trying to create a simple interface to my core data model in the style of iTunes Genre browser.

I have a model setup with three entities: Genre <-->> Artist <-->> Album.

I'd like to simply tie them each to a NSTableView, but it appears as though accessing children relationships from a NSArrayController is not KVC compliant. So, I'm having difficulty communicating the selected Genre objects to the ArtistController.

How do you do this? Is it even possible within IB without any custom subclassing?

Edit for Posterity: I was doing several things wrong.

  • The child controller needs to know about the managedObjectContext through its own binding.
  • The child controller must not be in Entity Mode, but rather operate as a NSMutableDictionary class.
  • And, finally, the child controller does not prepare its data. It retrieves it from the parent, through the Content Set binding. Use the controller key selection, and the model key path that connects to the children.

phew. Both Brian's answer and this MacResearch tutorial were helpful in determining my errors (and which parts I had right).


回答1:


The approach I would probably take is to have a separate NSArrayController for each table view, then have the content of one array controller be based on the selection of another.

For example, say you have table view A that displays the list of available genres, so it has an array controller A whose content is hooked up to your managed object context.

Then, say you have table view B which shows the available artists for whichever genre is selected in table A. This table would have its own array controller B, and the content array for controller B is bound so the "controller key" field in IB is set to the "selection" key of controller A, with "artists" being the model key (this assumes your Genre entity has a to-many relationship named "artists" to the Artist entity).

You can then apply the same principle to a third table view + controller to show the albums for the selected artist.

The general term for this kind of setup is a "master-detail interface", and is outlined in Apple's docs at this link



来源:https://stackoverflow.com/questions/1697872/implementing-parent-child-drill-down-in-cocoa-with-core-data-bindings-that-span

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!