Binding Custom Views: Code Sample
Binding Custom Views: Code Sample
- Subject: Binding Custom Views: Code Sample
- From: Mark Onyschuk <email@hidden>
- Date: Mon, 8 Mar 2004 15:42:39 -0500
From the README:
VSObjectController and VSArrayController are subclasses which add the
ability to bind custom views so that changes in the view are reflected
back in the model (as they are with standard views wired in Interface
Builder).
I coded up these examples based on some of the commentary on cocoa-dev,
and because I intend to use them myself. Use them as you like, but if
you have any enhancements or fixes (the array controller subclass is
included, though I'm not using it right now, I don't guarantee that it
works, though I expect that it does) please consider emailing
monyschuk@voxsoftware with the fix. I'll update the sources with your
fix or enhancement.
To bind a custom view object to either of these subclasses, use the new
controller method:
- (void)bindObject:(id)anInterfaceObject
withKey:(NSString *)aKey
toKeyPath:(NSString *)aKeyPath
options:(NSDictionary *)options;
aKey is an attribute of your interfaceObject. aKeyPath is a keyPath on
the controller. In case your view object goes away during the life of
the controller, call the following method to break the association:
- (void)unbindObject:(id)anInterfaceObject withKey:(NSString *)aKey;
Both ObjectController and ArrayController subclasses keep an array of
helper objects that observe the view for changes in bound attributes.
When a view's bound attribute changes, the helper object sees the
change and messages the controller, updating it's keypath attribute -
which in turn updates the model.
The "nice thing" about this sort of arrangement is that you can drop
these two subclasses into your project, wire things up with the
above-mentioned methods, and all of a sudden all of the custom views
you've coded can behave like the IB-wired views that both update when
the model changes, and update the model when you change view
attributes. You don't need to change your custom views at all to take
advantage of this.
You can find the code at the following URL:
http://www.voxsoftware.com/Examples/TwoWayBindings.zip
-Mark
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.