views:

20

answers:

0

I'm trying to make a view class which exposes bindings. I've got the simple case for bindings worked out: the following code exposes two bindings. As long as I make properties with the same name as the bindings, all works.

+ (void)initialize
{
    if (self == [RecipeView class])
    {
        [self exposeBinding:@"name"];
        [self exposeBinding:@"author"];
    }
}

Now, I want to have a binding that takes an array. My view will display each item in the array and let them be edited. I can't do that in the same manner (I don't think), because the binding would represent the entire array, so any edit made would mean wiping and starting again.

When using bindings in IB, one can choose a controller key and a model key path. It is common to use a controller key like arrangedObjects (on an NSArrayController), and a model key path on something like objectTitle (a string property). That would give my binding an array of strings. If I used the simple binding method above, where the user making a single change would mean I updated my view's property with a new NSArray, would the NSArrayController know how to link the items in the new array back up to the original objects?

I am aware that one can make bindings with the method bind: toObject: withKeyPath: options: as well. This, I take it, lets more complicated bindings be created.

From this, I am passed a binding name, a controller object to bind to, a key path to follow, and some options. In my example, the controller object will probably be an NSArrayController, and the key path will be @"arrangedObjects.objectTitle". But what do I do then? I could keep hold of this object, observe it for changes, and then just lop off the last component of the keypath, use the remainder to obtain the array of objects themselves, and make my changes directly to them using setValue:forKey:.

Is that what I'm supposed to do? Manually mucking about with the keypaths like that seems a bit weird. (It's a damn shame that you get the controller key and the model key path concatenated like that. If they were separate, this form of observing would be easier.)

Sorry for this rambling essay. If anyone can help me write a view that can bind to an array of objects and edit one of their properties, please let me know!


Edit to add: I've just read something about how the best way to do this is to observe the entire collection, and also to separately observe the property we're interested in on each object. When a new object is added to the collection, start observing the interested property on that object, and so forth. It looks promising… is this along the right lines?


Edit 2 (sorry!) The popup menu takes one binding to the objects that back the menu (content), and one to the specific keypath to display (contentValues). I could go down that route, which would save me messing with the keypath in order to know what to observe. How come NSTableColumn doesn't need to do this?