R2D2
R2D2

Reputation: 2640

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...

Upvotes: 0

Views: 102

Answers (1)

Xu Yin
Xu Yin

Reputation: 3940

there is a couple of extra steps you have to do:

  1. convert the string value of your text filed into numerical value
  2. do the math there
  3. 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.

Upvotes: 1

Related Questions