0
是否可以爲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;
}
好的,謝謝丹,有可能你可以幫我解決這個問題。 – Sleeperinn
你需要設計一些方案,比如使用'UITextFieldDelegate'標記最後一個輸入文本的文本框,或者像用戶開始編輯一個文本框時的任何操作一樣,清除所有這些文本框,然後當用戶按下按鈕,只有最後一個編輯過的文本框中會有文本,所以你可以假設這是計算的基礎文件 –
我在這個編程中很新穎,是否有可能幫助我解決一些代碼讓我走吧。 – Sleeperinn