Re: A question about binding and use
Re: A question about binding and use
- Subject: Re: A question about binding and use
- From: Scott Ellsworth <email@hidden>
- Date: Mon, 7 Mar 2005 12:39:02 -0800
FROM : beauvais
DATE : Mon Mar 07 15:41:44 2005
[...]
Bindings are not a magic bullet - they exist to replace the glue code
in your app, and to provide a very fast notification service. When
your app is doing something other than those two tasks, bindings do not
help.
How would you add customization of an action when you are trying to
take advantage of the Cocoa bindings.
The action would be just like it would have been prior to bindings,
save that it does not change the values of screen objects, it changes
the values of objects that the ui is observing.
For example: Assume your app is just a user visible text label, a text
field, and button. When the button is pressed, the value is copied
from the text field to the string,
Prior to bindings, you would have a the string and the text field as
IBOutlets. Your IBAction procedure would call setText on the string,
using the result of getText on the entry field.
Your new code is very similar, save that you bind the contents of the
text field to some NSString *, perhaps called fieldValue, in your
document and the string to an NSString * in your document, perhaps
called labelValue. Your action procedure merely sets the labelValue
from fieldValue. You can write a setter method, call setValue:forKey:,
or edit the underlying field directly after bracketing it with calls to
will/didChangeValueforKey:
In other words, assuming fieldValue and labelValue accessors, and
appropriate bindings hooking up the field/label GUI elements to the
underlying objects in MyDocument, the method is just:
- (IBAction)doButton:(id)sender
{
[self setLabelValue:[self fieldValue]];
}
The bindings mechanism will handle the needed notifications.
Why is this useful? Because you then are working with objects in the
model, rather than notifications in the controller. Less to screw up,
and probably where you should be working anyway.
For example I'm writing an enterprise application which accesses a
MySQL database. When a button is selected, i.e. IBAction, I will need
to make MySQL accesses to the database to pull data and then present
it in textfields, etc.
When the button is pressed, you write the procedure as you would have
before. The difference is that you set values directly, rather than
setting text fields. The text fields are bound to the underlying
variables.
Maybe the answer is you can't use binding when you need to do
something as specific as this.
You can, but you are only using bindings to manage the ui state, not
the underlying model. For that, wait for Core Data in Tiger, though
note that it is not targeted at client server applications.
<http://developer.apple.com/macosx/tiger/> Thus, you use bindings to
get values from objects in your model to elements in the GUI and vice
versa, and you use ObjC code in action methods to do the data
manipulation.
Scott
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden