0

我有 2 个uitextfields(dollarPayTextField 和 rielPayTexField)。输入完成后dollarPayTextField,我想rielPayTextField自动计算。

例如,total: 10 $,当我在 DollarPayTextField 中输入 9 $ 时,我希望 rielPayTextField 显示 4000。那么,我该怎么做呢?这是我计算剩余的代码:

-(void)updateChangeRemainInriel{
    double dollarPay = [self.dollarPayTextField.text doubleValue];
    double dollarRemain = [self.abill getTotalPrice] - dollarPay;
    NSLog(@"%.2f",[self.dollarPayTextField.text doubleValue]);
    self.rielPayTextField.text =  [NSString stringWithFormat:@"%.2f",dollarRemain * self.anExchangeRate.rielBuyPerDollar];
}
4

2 回答 2

0
  - (void)textFieldDidBeginEditing:(UITextField *)textField;
        {
               self.txtRiel.text=textField.text*100;// your calculation over here
        }

另请参阅委托方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;

请参阅此处的 Apple 文档

于 2012-08-29T04:41:01.243 回答
0

希望您在 .h 文件中添加UITextFieldDelegate和your_textField.delegate=self; 在您的 .m 文件中。如果使用它,它会在每次输入更改时计算

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

   [textField addTarget:self action:@selector(yourTextFieldChanged:) forControlEvents:UIControlEventEditingChanged];  

    return YES;
}

- (void)yourTextFieldChanged:(UITextField *)textField
{
   double dollarPay = [self.dollarPayTextField.text doubleValue];
   double dollarRemain = [self.abill getTotalPrice] - dollarPay;
   NSLog(@"%.2f",[self.dollarPayTextField.text doubleValue]);
   self.rielPayTextField.text =  [NSString stringWithFormat:@"%.2f",dollarRemain *    self.anExchangeRate.rielBuyPerDollar];
}

如果你使用它,它会在你输入完值后计算

-(void)textFieldDidEndEditing:(UITextField *)textField{
   double dollarPay = [self.dollarPayTextField.text doubleValue];
   double dollarRemain = [self.abill getTotalPrice] - dollarPay;
   NSLog(@"%.2f",[self.dollarPayTextField.text doubleValue]);
   self.rielPayTextField.text =  [NSString stringWithFormat:@"%.2f",dollarRemain *    self.anExchangeRate.rielBuyPerDollar];
}
于 2012-08-29T04:52:58.030 回答