views:

403

answers:

2

I've got an NSOutlineView acting as a source list for my application, and my data model is done with Core Data. I'd like to use bindings (if possible) to glue these things together as follows:

I have three main entities in my model, for sake of example let's call them "Stores", "Cars" and "People".

My goal is to have the outline view have 3 "groups" (expandable nodes, like PLAYLISTS in iTunes), each group representing and listing one of my entities, and also I've got three buttons at the bottom of my window to "Add Store", "Add Car", etc which I'd like to have wired up to perform that action.

So far in my window's nib I've got a TreeController which is bound to my NSManagedObjectContext instance of my window controller, but I can't figure out how to properly bind and populate the outline view from the TreeController.

Is this possible with bindings? I've seen one tutorial where a second managed object model is created, with entities for the outline nodes, but some comments on the article said this was a bad idea. I'm not really sure how to proceed, any help would be wonderful!

+1  A: 

This is a pretty common pattern (and has resulted in duplicate questions). A general approach is given here:

http://stackoverflow.com/questions/1812311#1812924

Joshua Nozzi
I had previously seen this question on StackOverflow and the reason why I still asked is because my app does not have a section of "Fixed" items (unless you consider my 3 group titles to be fixed). Also, the linked solution doesn't use bindings at all. I'm trying to avoid implementing the datasource methods by hand if possible, as it's confusing and error-prone.
jbrennan
+2  A: 

Try the Cocoa Bindings Programming topics: Providing Controller Content section

What I did was create custom classes for my entities, and added isLeaf properties to them. For the top level (Stores, Cars, People) I return no. For leaf nodes (a car, a person, etc) I return YES.

The top level needs to have a to-many relationship to the leaf nodes, I called this children.

In Interface Builder, I set the NSTreeController's mode to Entity, name: Groups. It's bound to the managedObjectContext. In the Key Paths I set the Children attribute to children, and Leaf to isLeaf.

ctshryock
So my top-level entities I would call `Groups`, which would have a `name` attribute as well, and then all the leaf nodes would be my `Car` `Store` and `Person` entities, is this correct? Finally, I already have these entities in my model (for actually modelling the data), should I just use those, or should I have like `CarNode` `PersonNode` instead?
jbrennan
Sorry, I mean the top-level nodes in the Outline view would be represented by an entity called `Groups`, is that also correct?
jbrennan
I would say an entity called `Group` whose name values would be `Cars`, `Stores`, `People`, yes. That entity has a relationship to the other entities, called `children`. You could use your existing entities, but I made custom subclasses of `NSManagedObject` for them to add the `isLeaf` properties. I could post my demo app if you're interested
ctshryock
If you wouldn't mind posting it that would be fantastic, just to I can look at how everything is wired up :)
jbrennan
Here ya go:http://ctshryock.com/swag/UserDefaults.zipThis is just a demo app I threw together to learn using NSTreeController and tinker with NSUserDefaults (hence the name).You can ignore the files that start with the underscore, I was tinkering with MOGenerator.
ctshryock