views:

1161

answers:

2

hello,

I'm trying to find a way to link an array controller to an Array. I know via an outlet I can fill textfields and even tableviews in the .xib file made with Interface Builder.

I was hoping it would be possible to have one easy outlet (NSArray) and put it into an Array Controller. In turn, a table will link its contentfield to the stuff in the Array. Is this possible or am I just thinking too much non-cocoa?

Right now I have an Object Reference in Interface builder which already passes through a pair of fields and some action handers to buttons. If I try to link the Array Controller to the outlet 'FilesArray' I can only link it to the Object itself, not its outlets.

I'm only a beginner so I don't see why this wouldn't be possible?

thanks!

+1  A: 

You just need to go to the Bindings Pane in the Interface builder inspector and under the "Controller Content" for a NSArrayController you need to bind to the array you want. Then you can just bind the NSTableColumn to the NSArray Controller.

See With and Without Bindings for an example of using NSArrayController.

Also CocoaDev gives some more precise instructions on using NSArrayController and using it with TableViews http://www.cocoadev.com/index.pl?NSArrayController

Colin Wheeler
A: 

You don't use an outlet for this. Instead, you go the other way, and bind the array controller to a property of your object.

  1. Delete the IBOutlet keyword.
  2. In the header, declare the instance variable as an NSMutableArray.
  3. In the implementation, create the array in init and release it in dealloc.
  4. In the header, declare a property for the array. Make sure to declare it with the copy attribute.
  5. In the implementation, synthesize accessors for the property.
  6. In IB, set the array controller's Content Array binding. Bind it to the object that has your array property. Set the model key path to the name of the property you created in Xcode.
  7. In your implementation, always use accessors to mutate the property (e.g., to add or remove objects).
Peter Hosey