Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Multiply two values

I'm making my first iOS app. I simply want to multiply the value from pressureField with temperatureField. I used a helloiPhone.app to get things going. So everything is working if I use one field, but I don't know how to set the value of h to two multiplied vars.

Here's what my mainview.m looks like:

- (void) sayHello: (id) sender {
    h = 23;
    helloLabel.text =
        [NSString stringWithFormat: @"%.2f", h];
    [pressureField resignFirstResponder];
    [tempField resignFirstResponder];
    tempField.text = @"";
    pressureField.text = @"";
}

I'm NEW to Objective C...so be easy on me. I'm just hobbying. :)

like image 760
Kevin Brown Avatar asked Feb 13 '11 22:02

Kevin Brown


1 Answers

If you're talking about how to get the values and convert them to a numeric format, then something like the following should do the trick...

// Grab the values from the UITextFields.
float pressureValue = [[pressureField text] floatValue];
float temperatureValue = [[temperatureField text] floatValue];

// Multiply them.
float h = pressureValue * temperatureValue;

You should be able to simply replace your initial h = 23; line with the above. (That said, I've not checked the above in a compiler, but it's pretty simple so it should be OK.) :-)

What you're doing here is getting the textual value from the UITextFields (that you've presumably set up as IBOutlets in your interface file), which are supplied as a NSString pointer via the UITextField text method. We're then using the NSString floatValue method to obtain the value in a suitable numeric format.

Incidentally, I'd really recommend a good read of Apple's class reference documentation (such as the UITextField & NSString docs linked above) - it's very good quality and you'll learn a lot from simply looking at some of the available methods.

like image 186
John Parker Avatar answered Sep 28 '22 15:09

John Parker