Search code examples
objective-cxcodemacoscore-datacocoa-bindings

Change contents of NSTextField depending on selected item of NSTableView using the same Array Controller


I have a Core Data Entity named Toys with two attributes: an integer named identifier and an NSString named details.

My storyboard setup consists of a split view with an NSTableView on the left and an NSTextField on the right. The table on the left is displaying a list of the existing toy objects with their identifier integers, and I'd like to change the content of the text field on the right so it corresponds to the details of the object selected on the table.

I'm trying to achieve that by using Cocoa Bindings. To set up the table, I created an Array Controller connected to the Toys entity. To hook up the NSTextField, I bound the value of the field to that same array controller.

Currently, the table will gather a list of all my objects but the text field will load the description for a random object of the array each time I run the application. How could I change the contents of the text field based on the row selected on the left side? I'd prefer to use bindings if available.

example


Solution

  • Thanks for the comments. I was indeed missing a connection from the table to the array controller. Fixed by binding the table to the array controller under Selection Indexes, with selectionIndexes set as Controller Key.