Search code examples
cocoacocoa-bindings

Connect NSOutlineView/NSTreeView into an NSArrayController?


I'm trying to use Bindings with NSOutlineView.

I cannot see how to connect the "currently selected item" to anything else via Bindings

I've tried:

  • use NSOutlineView directly, bind to "selection" (doesn't work; looking at the docs, Apple hasn't implemented that binding)
  • use an NSArrayController, bind to "arrangedObjects", and bind the NSAC to the NSOV's "selectedRowIndexes" (doesn't work; you get a strange error message that "NSSet cannot be created from NSIndexSet"; (why not? surely, one is a subclass of the other?)

Solution

  • You don't use NSArrayController for an outline view, you use an NSTreeController. Then you can just bind to yourTreeController.selectedObjects if you want to access the selection.

    NSArrayController is for use with NSTableView, NSTreeController is for use with NSOutlineView.

    Apple has a great example showing how to use NSOutlineView with NSTreeController.