开发者

my first iPhone app and i'm stuck

开发者 https://www.devze.com 2022-12-21 17:25 出处:网络
i\'ve decided to give this iPhone App development a kick. to help add perspective to this i\'ve never programmed in any form of C or for any anything on the Mac other than Applescript. i\'ve created p

i've decided to give this iPhone App development a kick. to help add perspective to this i've never programmed in any form of C or for any anything on the Mac other than Applescript. i've created plenty of solutions using blends of Applescript, Classic ASP, Perl, even PostScript. i'm home brewed so i don't understand all the my Obj.xyz.blah.blee stuff, sorry ;-/

i need to... 1) take data from a field 2) convert it to a float 3) do some math 4) then plop the results in a label

i've gotten a version of this working where i pull the text from a filed and plop the results into a label upon clicking on a "Calculate" button, that was easy. so i know that points 1 and 4 are good. now for the tough part...

i've setup an initial version to ensure i can do points 3 and 4 by setting my float variables as static bits of data...

float float_DiameterA, float_DiameterB;
float myR1, myR2;

float_DiameterA = 20;
float_DiameterB = 5;

myR1=(float_DiameterA-float_DiameterB)/2;
myR2=1;

r1.text=[[NSNumber numberWithFloat:myR1] stringValue];
r2.text=[[NSNumber numberWithFloat:myR2] stringValue];
a1.text = txtLength.text;

results : r1 displays "7.5" and r2 displays "1". this works perfectly BUT i need to take txtDiameterA.text and put it into float_DiameterA as a floating point.

here's what i've tried for step 2...

str_DiameterA = [txtDiameterA.text text];
float_DiameterA = [[str_DiameterA text] floatValue];

but that didn't work ;-/

i've even tried several versions of if(EOF == sscanf(str_DiameterA, "%f", &float_DiameterA)){ //error };

and

float_DiameterA = sscanf(txtDiameterA.text, "%f", &f);

but none of those worked either. i think that's when i started to realize this wasn't C++ as i thought it was lol...

think you'll get the idea by now.

so...

how do you take the input from txtDiameterA.text and convert into a float? please don't tell me to use Root Beer instead of a开发者_开发知识库 Cola lol.

oh, here's a few more bits of information that might have some impact...

part of my .h file

IBOutlet UITextField *txtDiameterA;
IBOutlet UITextField *txtDiameterB;

IBOutlet UILabel *r1;
IBOutlet UILabel *r2;

i designed my UI using the Interface Builder.

THANK YOU VERY MUCH FOR YOUR ASSISTANCE!!! ;-)


txtDiameterA is a UITextField. Looking at the docs we can see that the 'text' attribute of a UITextField returns an NSString

NSString has a lovely method called floatValue that returns a float representation of the string!

So,

NSString *diameter = txtDiameterA.text;
float diameterAsFloat = [diameter floatValue];

gives you the value in your text field as a float.

0

精彩评论

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