0

I am using several UITextFields and want to do anytime anything changes in any one of them.

It looks basically like:

@IBOutlet weak var f1: UITextField!
@IBOutlet weak var f2: UITextField!
@IBOutlet weak var f3: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    f1.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
    f2.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
    f3.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
}

@objc func textFieldDidChange(_ textField: UITextField) {
    // Data in one of the fields changed.
    if self.view.tag == 1 {
        print("field 1")
    } else {
        if self.view.tag == 2 {
            print("field 2")
        }
        //...
    }
}

As you can see, I've tried setting tags, but not sure how to access them here.

I’m using XCode 9.01 and Swift 4.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Greg
  • 667
  • 8
  • 19

2 Answers2

3

You don't need tags since you have outlets. Just compare the textField parameter to each of your outlets:

@objc func textFieldDidChange(_ textField: UITextField) {
    if textField == f1 {
        print("field 1")
    } else if textField == f2 {
        print("field 2")
    } else if textField == f3 {
        print("field 3")
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Using a `switch` wouldn’t improve readability a little? Anyway, might be just a matter of taste really... ;) – Paulo Mattos Oct 27 '17 at 16:59
  • @PauloMattos Using a `switch` is another option. Feel free to post an answer showing that solution. I'm a long time Objective-C programmer and I'm still not used to being able to use `switch` for things like this. – rmaddy Oct 27 '17 at 17:01
3

A quick, modest improvement over rmaddy’s answer:

@objc func textFieldDidChange(_ textField: UITextField) {
    switch textField {
    case f1: print("field 1")
    case f2: print("field 2")
    case f3: print("field 3")
    default: fatalError(“Unknown field: \(textField)”)
    }
}

Besides using a switch — which might slightly improve code readability one may argue — this version also triggers a runtime error if an unexpected text field is passed. Luckily, this might just catch a programming error later on ;)

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
  • 1
    Personally, I'd change your opening statement to "A quick, alternate solution to rmaddy's answer". Calling it a "modest improvement" is subjective and a matter of taste. :) But it still gets a +1 from me. – rmaddy Oct 27 '17 at 17:17
  • 1
    Perfect! Yes, a switch was coming down the road, this was only to figure out how it worked. Thank you everyone! – Greg Oct 28 '17 at 14:05
  • Hey @Greg, if you went with the `switch` version you should really pick my answer as the accepted one bro ;) Thanks and good luck! – Paulo Mattos Oct 28 '17 at 14:17
  • In all fairness, rmaddy's was first and I had already planned to use switch for what was to come. – Greg Oct 28 '17 at 18:40