3

(Abstract: bindings work in code, but not in IB)

I have a window managed by a NSWindowController. To the left of the window is a source view. To the right is a table view showing the elements of the currently selected source.

I have set up a NSTreeController within my window XIB. I want its contents to be used for the source view. It's selection will drive the table view.

I am trying to split this up using NSViewControllers. One view controller will load a NIB containing the source view. Another view controller will load the table view.

Seeing that I need access to the NSTreeController within the source view controller, I have set it to be the view controller's representedObject. (Actually for this setup to be done by the time awakeFromNib is called on the view controller, I have turned representedObject into an IBOutlet).

All works fine when I wire my source view up in code:

[outlineView bind:@"content"
         toObject:sources
      withKeyPath:@"arrangedObjects"
          options:nil];
[outlineView bind:@"selectionIndexPaths"
         toObject:sources
      withKeyPath:@"selectionIndexPaths"
          options:nil];
[[outlineView tableColumnWithIdentifier:@"Title"] bind:@"value"
                                              toObject:sources
                                           withKeyPath:@"arrangedObjects.title"
                                               options:nil];

I am however unable to reproduce this using Interface Builder. Thing is, here the "controller key" textfield is grayed out. Thus I bind column's "value" to the file owner using a model keyPath of "representedObject.arrangedObjects.title". This does not show the desired behavior. Actually an exception is thrown: -[NSProxy doesNotRecognizeSelector:_mutatingNodes] called!

How can I use representedObject in IB? Can I create a controller in IB which acts as proxy to representedObject? Could I set-up a tree controller in the source view XIB which during NIB loading gets swapped out for the representedObject?

Pierre Bernard
  • 3,148
  • 2
  • 23
  • 31

2 Answers2

2

I moved away from using representedObject. It appears that is meant only for model objects.

I now pass in my tree controller using a custom outlet. I continued setting up and tearing down the bindings in code.

Pierre Bernard
  • 3,148
  • 2
  • 23
  • 31
0

I’ve similar issues when I try to pass a reference to an object controller (NSTreeController in my case). I don’t think this is how Apple wants you to use their KVO-compatible controllers. The exceptions look like they’re XIB-unarchiving & timing-related.

The trick is not to pass the controllers, but to pass the underlying data and keep the selection in sync.

This way you can set up your bindings in a storyboard and won’t get any exceptions. You’ll have to set up a new instance of an object controller for every child view controller (copy & paste in Storyboard once you configured the first one works). For a detailed example take a look at another answer that gets much more into detail.

Rafael Bugajewski
  • 1,702
  • 3
  • 22
  • 37