是否可以为 3 个文本框设置 1 个计算按钮,或者我是否需要为每个文本框设置一个按钮,文本框是桶、加仑和升,如果我在桶框中输入 10,我希望按钮计算多少加仑和升 10 桶。同样,如果我将 10 放入升框中,我希望按钮计算多少桶和加仑。这是我现在的代码:
- (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;
}