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!

jbrennan
  • 11,943
  • 14
  • 73
  • 115

1 Answers1

2

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.

catsby
  • 11,276
  • 3
  • 37
  • 37
  • Sorry, I mean the top-level nodes in the Outline view would be represented by an entity called `Groups`, is that also correct? – jbrennan Dec 23 '09 at 21:38
  • 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 – catsby Dec 23 '09 at 22:03
  • If you wouldn't mind posting it that would be fantastic, just to I can look at how everything is wired up :) – jbrennan Dec 23 '09 at 22:09
  • Here ya go: http://ctshryock.com/swag/UserDefaults.zip This 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. – catsby Dec 26 '09 at 01:00
  • @catsby, http://ctshryock.com/swag/UserDefaults.zip - unfortunately, 404 Not Found :( (just looking for a working example) – Dmitry Isaev Jan 05 '15 at 09:58
  • @DmitriyIsaev I posted it in 2009 so it likely doesn't work anymore anyway :) – catsby Jan 05 '15 at 14:39