adding numbers in uitextfield to update uilabel
I have code with several uitextfields that will be used to input numbers, and I want to add these numbers together to update a uilabel.
I can do all the updating and the labels and fields, but can't get the addition to work.
Just now I have:
label.text = (textfield1.text + textfield2.text);
I assume I need to convert these textfield inputs to an int, but not sure how to do that...
there is a couple of extra steps you have to do:
- convert the string value of your text filed into numerical value
- do the math there
- and convert it back.
For example (i use float in my case, you can change that to whatever type you want):
float textField1Value = [textfield1.text floarValue]; float textField2Value = [textfield2.text floarValue]; label.text = [NSString stringWithFormat:@"%f", textField1Value + textField2Value];
Hope that helps.