Objective c 3个文本框和一个计算按钮
我有3个文本框和一个计算按钮,我如何才能告诉按钮开关文本框被选中,并转换成其他文本框的数字,我已经标记了文本框1,2和3,我是非常新的和绿色的编程,所以任何帮助将是伟大的。 这是我的密码Objective c 3个文本框和一个计算按钮,objective-c,xcode4.6,Objective C,Xcode4.6,我有3个文本框和一个计算按钮,我如何才能告诉按钮开关文本框被选中,并转换成其他文本框的数字,我已经标记了文本框1,2和3,我是非常新的和绿色的编程,所以任何帮助将是伟大的。 这是我的密码 - (IBAction)Calculate:(id)sender { NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; float a = [[numberFormatter numberFromStr
- (IBAction)Calculate:(id)sender {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
float a = [[numberFormatter numberFromString:_Barrels.text] floatValue];
float b = [[numberFormatter numberFromString:_Gallons.text] floatValue];
float c = [[numberFormatter numberFromString:_Liters.text] floatValue];
_Barrels.text = [[NSString alloc]initWithFormat:@"% .2f", a];
_Gallons.text = [[NSString alloc]initWithFormat:@"% .2f", a * 42];
_Liters.text = [[NSString alloc]initWithFormat:@"% .2f", a * 159];
_Barrels.text = [[NSString alloc]initWithFormat:@"% .2f", b * .0238];
_Gallons.text = [[NSString alloc]initWithFormat:@"% .2f", b];
_Liters.text = [[NSString alloc]initWithFormat:@"% .2f", b * 3.785];
_Barrels.text = [[NSString alloc]initWithFormat:@"% .2f", c * .0063];
_Gallons.text = [[NSString alloc]initWithFormat:@"% .2f", c * .264];
_Liters.text = [[NSString alloc]initWithFormat:@"% .2f", c];
switch ([sender tag]) {
case 1:
[_Barrels resignFirstResponder];
[_Gallons resignFirstResponder];
[_Liters resignFirstResponder];
break;
default:
break;
}
在ViewController.h文件中使用一个变量/标志来存储最后选择的文本字段
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
{
int textfieldFlag;
}
@end
那么在乌尔
- (IBAction)Calculate:(id)sender
{
//ABove Code....
if(textfieldFlag==1){
_Barrels.text = [[NSString alloc]initWithFormat:@"% .2f", a];
_Gallons.text = [[NSString alloc]initWithFormat:@"% .2f", a * 42];
_Liters.text = [[NSString alloc]initWithFormat:@"% .2f", a * 159];
}
if(textfieldFlag==2){
_Barrels.text = [[NSString alloc]initWithFormat:@"% .2f", b * .0238];
_Gallons.text = [[NSString alloc]initWithFormat:@"% .2f", b];
_Liters.text = [[NSString alloc]initWithFormat:@"% .2f", b * 3.785];
}
if(textfieldFlag==3){
_Barrels.text = [[NSString alloc]initWithFormat:@"% .2f", c * .0063];
_Gallons.text = [[NSString alloc]initWithFormat:@"% .2f", c * .264];
_Liters.text = [[NSString alloc]initWithFormat:@"% .2f", c];
}
//Below Code...
}
相应地使用textfieldFlag
textfieldFlag
将包含上次选择/编辑的textfield的标志当您点击计算按钮时,必须调用此方法:-(void)textfieldDendediting:(UITextField*)textfield
。在这里,通过检查tagValue来确定编辑了哪个文本字段,并据此进行计算。请确保添加文本字段的代理,否则-(void)textfielddidediting将不会被调用。您是否有可能在我的代码中设置此选项,以便我可以看到它,我只是在这里遇到了很多错误:/对不起,我的回答很糟糕。。int textField=0;在.h中是错误的,请查看编辑过的答案OK,它现在正在运行,但是如果我在其中一个文本框中输入一个数字,然后在其他文本框中单击“计算无任何内容”,您知道可能有什么问题吗?检查您是否已为文本字段设置了委托,并且正在调用textFieldDidBeginEditing
方法。
- (IBAction)Calculate:(id)sender
{
//ABove Code....
if(textfieldFlag==1){
_Barrels.text = [[NSString alloc]initWithFormat:@"% .2f", a];
_Gallons.text = [[NSString alloc]initWithFormat:@"% .2f", a * 42];
_Liters.text = [[NSString alloc]initWithFormat:@"% .2f", a * 159];
}
if(textfieldFlag==2){
_Barrels.text = [[NSString alloc]initWithFormat:@"% .2f", b * .0238];
_Gallons.text = [[NSString alloc]initWithFormat:@"% .2f", b];
_Liters.text = [[NSString alloc]initWithFormat:@"% .2f", b * 3.785];
}
if(textfieldFlag==3){
_Barrels.text = [[NSString alloc]initWithFormat:@"% .2f", c * .0063];
_Gallons.text = [[NSString alloc]initWithFormat:@"% .2f", c * .264];
_Liters.text = [[NSString alloc]initWithFormat:@"% .2f", c];
}
//Below Code...