Re: Binding a model's ivar array to the contents of an NSArrayController
Re: Binding a model's ivar array to the contents of an NSArrayController
- Subject: Re: Binding a model's ivar array to the contents of an NSArrayController
- From: Quincey Morris <email@hidden>
- Date: Wed, 16 Sep 2009 11:11:04 -0700
On Sep 16, 2009, at 08:29, A B wrote:
I have an NSView that is keeping track of a group of objects via an
NSMutableArray ivar (let's call it relatedObjects). Objects in this
array are represented in the view by subviews (i.e. when I add an
object to the array, a new subview appears, when I delete them, the
associated subview is removed, etc.) I'd like to expand the
functionality of my NSView class by letting that array of objects be
bound to an NSArrayController... unfortunately, I'm stymied by the
very first step: That of actually getting relatedObjects to mirror
the contents of the controller.
So in short, my need is pretty simple: Bind an array to an
NSArrayController. That being said, it seems that no combination of
exposeBindings:, bind:toObject:withKeypath:options:,
observeValueForKey:ofObject:change:context:, etc. is working as I
expect it to. Admittedly, I have done most of my binding work via
IB and I've never really done much with
bind:toObject:withKeypath:options: before, so I wouldn't be
surprised to find that that was where my problem lay.
Let's straighten out the terminology first. You can't "bind an array
to" an object. You bind one of an object's named bindings to a
particular property of another object. For example, NSButton objects
have a "value" binding which can be bound to a numeric property of a
data model object.
So "binding" is actually used ambiguously in Cocoa. It refers to the
named capability (belonging to instances of a class) of being able to
be bound (to another object's property), *and* it refers the state of
(one object's) being bound (to another object's property).
Conceptually, the first meaning is vaguely analogous to a property,
the second to an IB connection.
The documentation doesn't really make this clear, but in order to use
the "bind:..." method successfully, you need the receiver to already
have a binding (in the first sense) defined on it. (You can consult
the receiver's list of exposed bindings to find out what you can use.)
"bind:..." doesn't create a binding, it uses an existing binding (in
the first sense), and establishes a binding (in the second sense).
If you want to use bindings to get array content into NSView, you
would first have to define the named binding, which includes providing
an implementation for it. This is a fiddly process (see the Cocoa
Bindings Reference for a description of what's involved), and it's
probably not worth the effort.
Far easier would be to give the NSView an outlet to the
NSArrayController which you'd connect up in IB. Then, in your view's
"awakeFromNib" method, have it register itself as an observer of the
array controller's "arrangedObjects" property. In your
"observeValueForKey:..." method, update your view's array from the
NSArrayController.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden