views:

75

answers:

1

In my modell got an NSMutableArray that keeps track of a changing number of elements. In my view I got a NSTextField that shows the number of elements. The view gots unarchived from the nib file and alloc/inits the modell. Therefore it knowns about the modell and the contained array.

I established the connection as follows. In the Interface Builder at the textfield I added a Cocoa Binding "path" like this: myModell.myArray.@count. By this I can access the count property (which is a must since the array itself does not change). The binding is based on key-value compliance which I established at the modell so the array can be accessed. But key-value compliance is not part of the questions.

My question: How can I put the binding into the sourcecode and not writing it into the Interface Builder?

+2  A: 

With the NSKeyValueBindingCreation protocol. You send something like [someObject bind:@"value" toObject:myModel withKeyPath:@"myArray.@count" options:nil].

Chuck
I already tried this. To what would you refer someObject and "value" if I like to update myTextField? I tested the followings:[self bind:@"myTextField" ...][self bind:@"myTextField.@value" ...][myTextField bind:@"value" ...]None of the above worked though.
JJD
Nooooo, it obviously depends on where you are going to put the code, but assuming it is going somewhere with a valid reference to myTextField and myModel, the argument to bind: is the name of the binding. So as Chuck said.. [myTextField bind:@"value" toObject:myModel withKeyPath:@"myArray.@count" options:nil]
mustISignUp
Yes. Now I got it. [self.myTextField bind:@"stringValue" toObject:self.myModell withKeyPath:@"myArray.@count" options:nil]; Important to notice is that the textfield in IB needs to have some connection to the member variable myTextField too. (objectValue instead of stringValue works too) - Thanks guys!
JJD