I want to take two textfields values and add the values and show them into another textField.
我想获取两个textfields值并添加值并将它们显示到另一个textField中。
How to do this programatically?
如何以编程方式执行此操作?
2 个解决方案
#1
1
Follow this tip, then add the two NSNumbers.
按照这个提示,然后添加两个NSNumbers。
NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber * myNumber = [f numberFromString:ftext]; NSNumber * myNumber2 = [f numberFromString:ftext2]; float out = [myNumber floatValue] + [myNumber floatValue]; [f autorelease];
-dan
-担
#2
1
There is also a floatValue
method on NSString
so you can also do something like:
在NSString上还有一个floatValue方法,所以你也可以这样做:
float sum = [textField1.text floatValue] + [textField2.text floatValue];
Which is a little more concise.
哪个更简洁一些。
#1
1
Follow this tip, then add the two NSNumbers.
按照这个提示,然后添加两个NSNumbers。
NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber * myNumber = [f numberFromString:ftext]; NSNumber * myNumber2 = [f numberFromString:ftext2]; float out = [myNumber floatValue] + [myNumber floatValue]; [f autorelease];
-dan
-担
#2
1
There is also a floatValue
method on NSString
so you can also do something like:
在NSString上还有一个floatValue方法,所以你也可以这样做:
float sum = [textField1.text floatValue] + [textField2.text floatValue];
Which is a little more concise.
哪个更简洁一些。