1

I like to update textfield2 as soon as totalAmount has been edited, I am trying the following:

    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"
    }

And i am saving the text field values with

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()
    }
alex
  • 4,804
  • 14
  • 51
  • 86

3 Answers3

2

This can be done using UITextFieldDelegate Method use this function 1.)

textField1.delegate = self

2.)

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

now its upto you how you change the totalAmount.

Shamas S
  • 7,507
  • 10
  • 46
  • 58
baydi
  • 1,003
  • 6
  • 11
1

Use the delegate function of UITextField - textFieldDidEndEditing: for your textField1. Once in this function, you can get value of textField1 and you can edit textField2.

Apple Documentation for UITextFieldDelegate

Shamas S
  • 7,507
  • 10
  • 46
  • 58
0

Change the code to this totalAmount.addTarget(self, action: "changeTextField2:", forControlEvents: UIControlEvents.EditingChanged)

i.e change the UIControlEvents to "EditingChanged"

Nilesh Kumar
  • 2,141
  • 3
  • 16
  • 20