Two way binding in code

37 views
Skip to first unread message

Cristian Mardare

unread,
Oct 14, 2016, 12:02:20 PM10/14/16
to Cappuccino & Objective-J
Hi,

I'm fairly new to Cappuccino world, but I have a strong background in C-like programming language.
I'm having trouble understanding how binding works in Cappuccino and so far I haven't found an example on how to do a two way binding using code(not Xcode - I'm a windows developer... yeah, I know....).

What I've done so far: 

Initialise an CPDictionary with 2 keys.
Have 2 CPTextField and bind the "stringValue" to the properties of dictionary.
Set some values for the properties.

The TextFields update their stringValues according to the values set in dictionary.

Change the values in TextFields.

Nothing happens with the properties.

What am I missing here? 
Please provide a simple example that does what I want. Nothing fancy, I only need to grasp the concept, because I feel I'm really missing some basics here....

Thank you.

Prof. Michael Bach

unread,
Oct 14, 2016, 12:25:51 PM10/14/16
to objec...@googlegroups.com
Dear Cristian:

> I'm fairly new to Cappuccino world, but I have a strong background in C-like programming language.
> I'm having trouble understanding how binding works in Cappuccino …

It is much easier. Unter @implementation in the AppController or a separate class, declare:
CPString l1roundString @accessors; // sorry it has this strange name :)
// the @accessors is the magic for binding

later, when you want to change its values, use “set…” like so:
[self setL1roundString: [CPString stringWithFormat: @"%i%%", Math.round(100 * l1 / l2)]];
// note the use of a capital after “set”
// need to use “set” to make the binding actually work its magic
// this would not work: l1roundString = some string value

and in Xcode>Resources>MainMenu.xib do this:
select the textfield
do View>Utilities>Show Bindings Inspector
in the “value” field tick “Bind to” Delegate
in Model Key Path type
otView.l1roundString // I declared it in a class “otView” separate from AppController, OR just
l1roundString // if it’s declared in the AppController

That’s all! No dictionary necessary. You can find my example running here
http://michaelbach.de/ot/sze-tIllusion/
[you need to adjust the vertical blue line, then the button “show result” becomes enabled, clicking it changes the textbox under it via the “set…” command above.]


Welcome to Cappuccino, best, Michael
--
Prof. Michael Bach PhD, Eye Center, University of Freiburg, Killianstr. 5, 79106 Freiburg, Germany.
Michae...@uni-freiburg.de <http://michaelbach.de>

Martin Carlberg

unread,
Oct 14, 2016, 5:09:19 PM10/14/16
to objec...@googlegroups.com
Hi Cristian,

It is very easy as Michael is showing.

I have a very simple tutorial for a Cappuccino framework that includes bindings. You can find it here: https://github.com/mrcarlberg/LightObject.



Happy coding,

- Martin


--
You received this message because you are subscribed to the Google Groups "Cappuccino & Objective-J" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectivej+...@googlegroups.com.
To post to this group, send email to objec...@googlegroups.com.
Visit this group at https://groups.google.com/group/objectivej.
For more options, visit https://groups.google.com/d/optout.

daboe01

unread,
Oct 15, 2016, 2:43:32 AM10/15/16
to Cappuccino & Objective-J
hi christian,
you can find a 2-way binding example in code here:
best greetings,
daniel
Reply all
Reply to author
Forward
0 new messages