3

I have an inspector pane in my app that contains a bunch of controls. These controls are bound to my model objects through an NSArrayController. Depending on what type of object is selected I am displaying a different set of inspectors (just like how IB works). The inspector controller observes the array controller's selection, so that it can load the required set of inspectors when the selection changes.

The problem is that the old set of inspectors isn't removed apparently. Even through the inspector controller doesn't hold a strong reference to them and they are removed from their superview, they still stick around and log binding errors to the console:

[<Circle 0x102107df0> valueForUndefinedKey:]: this class is not key value 
coding-compliant for the key width.

My guess is that the NSArrayController holds a strong reference to the controls because of the bindings. Is this possible? Do I manually have to remove a binding before removing a control from the superview? How do I properly implement an inspector pane like this?


EDIT: The documentation says

Neither the receiver, nor anObserver, are retained.

so I guess bindings should be removed automatically when removing the control, shouldn't it?

DrummerB
  • 39,814
  • 12
  • 105
  • 142

1 Answers1

1

The problem is that there isn't a defined order between the inspector controller's response to the selection changing and the various inspector views updating themselves in response to the same thing. So, the "wrong" inspectors for the new array controller selection are still there for a brief time, at least, and trying to access non-existent properties of the element objects.

One fix would be to not rely on key-value observing the array controller selection to switch the set of inspectors in and out. Rather, have a coordinating controller – whichever is controlling the "selected object" based on the user action – clear the set of inspectors before changing the selection and not switching in the new set of inspectors until after it has been changed.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Yeah, in my experience you can either rely on KVO to structure your view hierarchy *or* you can bind things in the view hierarchy using KVO, but not both. In short, KVO gets confused if you add or remove observers from within a KVO notification handler. – ipmcc Aug 21 '13 at 12:00