Search code examples
objective-ccocoacore-datacocoa-bindingsnsarraycontroller

ArrayController's CoreData selection binding not refreshed across multiple NIB files


I've a hard time getting my Cocoa application to work as expected. It consists of a toolbar in the main.nib and a custom view in a details.nib file. Now I want the user to select an entry in a NSPopupButton in the toolbar and the content of the custom view should be changed accordingly.

To achive this I've added an ArrayController to my main.nib, showing the following configuration:

Attributes Inspector for Array Controller Managed Object Context Configuration

Furthermore the Managed Object Context is bound to the Model Key Path delegate.managedObjectContext (it is no document based application).

With this configuration the NSPopupButton works just fine and if I add a Label to the toolbar (also in the main.nib) and bind it's value to the selection (Controller Key), name (Key Value Path) the content is refreshed whenever I change the selection.

The Bindings of the NSPopupButton look like shown in the following screenshot: NSPupupButton Bindings

So in my details.nib I tried the following to achieve the same effect. I've added an ArrayController, whichs Managed Object Context is also bound to the Model Key Path delegate.managedObjectContext. Also the configuration is exactly the same as shown in the above pictures. I've then added a label and bound it's value to the selection (Controller Key), name (Key Value Path) of this ArrayController.

Label Bindings

The problem is that the Label only displays the the initial selection after the application did launched correctly. Afterwards, when I change the selection of my NSPopupButton, the label does not change accordingly.

enter image description here

What are my options to get the ArrayController working accross multiple NIB files?

BTW: I've tried to follow this blog post to get it working but it seems I'm missing something here.

Update: If I replace the Label in the details.nib by a NSTextField and change the text of it, the changes are reflected in the related NSPopupButton entry. So I guess I made something right, but the main problem remains: I can only edit the entry which was loaded during application startup. Switching to another NSPopupButton entry does not change the text in the NSTextField.

Update 2: I've created a small sample project with exactly the same configuration and uploaded it on GitHub. So feel free to check it out or create a pull request with a solution approach.


Solution

  • It seems you're missing the fact that, when you create the second array controller on the Details.xib it has no relation to the array controller on the MainMenu.xib. They are two separate instances.

    When you change the selection on the PopUp the only array controller affected is the one on MainMenu.xib.

    You have several options here:

    1. When you create your DetailViewController pass a reference to the array controller on the Controller and bind to that (don't create a new one on the details.xib)
    2. Just use simple KVO to observe the selection on Controller, and programatically change your label value.
    3. Just use simple KVO to observe the selection on Controller and update the array controller on the DetailsViewController to keep them in sync.
    4. your solution here...

    As long as you understand what's going on I'm sure you'll find the best solution to your original problem.