user437038
user437038

Reputation:

Add numerical content of UITextFields

I have four separate UITextFields and I want to add the numerical value of them all and then display the content within a UILabel, below is current code:

- (void)updateString {

    self.string1 = textField1.text;
    self.string2 = textField2.text;
    self.string3 = textField3.text;
    self.string4 = textField4.text;
    self.string5 = textField5.text;        

    label.text = self.total; // total is an NSString and label is a UILabel

}

I am unable to add together the numerical values within each textField1/2/3... and store the value within total and then update the label. Any suggestions?

Upvotes: 0

Views: 59

Answers (2)

fzwo
fzwo

Reputation: 9902

int totalValue = [textField1.text intValue] + [textField2.text intValue]...;

label.text = [NSString stringWithFormat:@"The total value is %d", totalValue];

Upvotes: 3

Simon Goldeen
Simon Goldeen

Reputation: 9080

NSString has a method on it -intValue. That is what you want to use.

Check the section "Getting Numeric Values" in the NSString documentation

Upvotes: 3

Related Questions