Paul Morris
Paul Morris

Reputation: 1773

Casting a UITextField value to an NSString

I am having a section of an application used for converting units from feet and inches to centimetres.

I accept the feet and inches from the user through two UITextField inputs which works fine. I then want to perform a calculation on the inputted values and display the result in a Label underneath. The code I am using for the calc when the 'convert' button is tapped is as follows;

-(IBAction)heighConvertButtonAction:(id)sender {

    NSString *feetCM = heightFeetField.text;
    NSString *inchesCM = heightInchesField.text;

    float fFeet = 30.48 * [feetCM integerValue];
    float fInches = 2.54 * [inchesCM integerValue];
    float fFeetInches = fFeet + fInches;
    fFeetInchesResult = [NSString stringWithFormat:@"%", fFeetInches];

    heightCMLabel.text = fFeetInchesResult;

}

It works in the sense that it doesnt produce any errors, however, when the convert button is touched it doesn't do anything. I was hoping it would populate the 'heightCMLabel' label with the result.

Any help would be appreciated.

Thanks

Upvotes: 0

Views: 1680

Answers (1)

MarkPowell
MarkPowell

Reputation: 16530

fFeetInchesResult = [NSString stringWithFormat:@"%", fFeetInches];

Not sure if it was just an error when posting your code, but that's not a valid string formatter.

Try:

fFeetInchesResult = [NSString stringWithFormat:@"%f", fFeetInches];

Upvotes: 2

Related Questions