In order to get started with bindings, I’m writing a small application that converts temperature in Celsius to temperature in Kelvin and vice-versa.
I have two NSTextFields which are binded to two properties known as float kelvin and float centigrade. And, of course, I have two custom setters which set the value for each property appropriately.
The setter for celsius is:
centigrade = value;
[self willChangeValueForKey:@"kelvin"];
kelvin = value + 273;
[self didChangeValueForKey:@"kelvin"];
I need to use willChangeValueForKey: otherwise the value is not updated in the other NSTextField.
My question is, is there a more elegant way to do this? If I add another property and textfield to convert the temperature to Fahrenheit, I will need to add willChange... and didChange... in that getter as well.
Is there a way to tell nib file that these two properties are linked and whenever one changes, it must notify both the observers?
EDIT:
For what its worth, I have tried calling the kelvin’s setter from inside centrigrade’s setter. But if I do that in the setter for kelvins as well, wouldn’t it cause problems? For instance:
centigrade = value;
[self setKelvin:value];
and
kelvin = value;
[self setCelsius:value];
Don’t use willChangeValueForKey: / didChangeValueForKey: here, the notifications are automatic. If you are not seeing the notifications it means you are not using the properties in a key-value coding compliant manner. You don’t need custom setters at all.
You should only call willChangeValueForKey: / didChangeValueForKey: if you have explicitly disabled automatic kvo for those properties (there are sometimes reasons to do this, but save that for later down the line).
Once you have that working you can tackle your other problem. Yes, it is possible to have one property change trigger notifications for both properties, but as you have seen that is no use if the second property change also triggers the first again, and so on.
The problem is that your Model isn’t sophisticated enough to deal with your problem. Forget bindings for a while, forget the presentation altogether until you have the model working. In pseudo-code, ignoring floating point details, you need to at least get something like this working:-
Assuming you follow the guides on kvo complience (and understand when to use it and when not to – ie, you are in control of when to trigger the notification), you will then be able to hook up your textfield bindings and everything will just work.
This might look something like:-
The key to making this work is to make sure that updating the value of temperatureKelvin sends a notification that temperatureFahrenheit and temperatureCelcius have changed. You do this by registering them as dependant preoperties..