Textfield nod adding together when called

My code is trying to take to textfields and add them together to a total textfield. If I assign the values in viewdidload it works but as soon as I change the values the score textfield is not updated.

    import UIKit

@IBOutlet var t11: UITextField!
@IBOutlet var t12: UITextField!
@IBOutlet var score: UITextField!

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

override func viewDidAppear(_ animated: Bool) {

func acc() {
    let dx = ((t11.text! as NSString).integerValue + (t12.text! as NSString).integerValue)
    print("its time",dx)
    score.text = String(dx)

Hi @zalubski

You can observe changes of your testfields like this:
yourTextfield.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)

And in that method you can update your ‘score’ textfield.


This topic was automatically closed after 166 days. New replies are no longer allowed.