开发者

iOS: Multiply two values

开发者 https://www.devze.com 2023-02-10 11:07 出处:网络
I\'m making my first iOS app. I simply want to multiply the value from pressureField with temperatureField.

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];
    tempFiel开发者_高级运维d.text = @"";
    pressureField.text = @"";
}

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号