如何根据编辑textfield1更新textfield2?

时间:2015-09-04 09:58:01

标签: ios swift addtarget

我想在编辑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()
    }

3 个答案:

答案 0 :(得分:2)

这可以使用UITextFieldDelegate方法使用此功能 1.)

textField1.delegate = self

2。)

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

现在可以了解如何更改totalAmount。

答案 1 :(得分:1)

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

Apple Documentation for UITextFieldDelegate

答案 2 :(得分:0)

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

即将UIControlEvents更改为" EditingChanged"