如何根据编辑 textfield1 更新 textfield2?

How do I update textfield2 based on editing textfield1?

我想在 totalAmount 被编辑后立即更新 textfield2,我正在尝试以下操作:

    totalAmount.addTarget(self, action: "changeTextField2:", forControlEvents: UIControlEvents.EditingDidEndOnExit)

 func changeTextField2(textField: UITextField) {
        print("Total amount: \(totalAmount.text)")
        //amount vat - calculate vat based on textField
        amountVAT.text = "88"
    }

我用

保存文本字段值
func newItem(){

    let context = self.context
    let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: context)


    let nItem = List(entity: ent!, insertIntoManagedObjectContext: context)
    let numberFormatter = NSNumberFormatter()

    nItem.amountVAT = numberFormatter.numberFromString(amountVAT.text!)
    .....more values
    nItem.invoiceImage = UIImagePNGRepresentation(imageHolder.image!)

    do {
     try context.save()
    } catch {
        print(error)
    }
}
func saveValues(sender: AnyObject) {

        if nItem != nil {
            editItem()
        } else {
            newItem()
        }

        dismissVC()
    }

为您的 textField1 使用 UITextField - textFieldDidEndEditing: 的委托函数。进入此函数后,您可以获取 textField1 的值并可以编辑 textField2。

Apple Documentation for UITextFieldDelegate

这可以使用 UITextFieldDelegate 方法使用此函数来完成 1.)

textField1.delegate = self

2.)

func textFieldDidEndEditing(textField: UITextField)
{
   if textField == textField1
   {
      textField2.text = textField1.text
   }
}

现在由您决定如何更改总金额。

将代码更改为此totalAmount.addTarget(self, action: "changeTextField2:", forControlEvents: UIControlEvents.EditingChanged)

即将 UIControlEvents 更改为 "EditingChanged"