0

I have a UITextField that has a target added which performs checks on the field as the user is typing. I currently have an issue however when my code adds text to the textfield in that the text doesn't get checked. Is there a way I can solve this through .editingChanged or is there another UIControlEvent to hook into?

Code is:

NumberOutlet.addTarget(self, action: #selector(handleNumberImage), for: .editingChanged)
ronatory
  • 7,156
  • 4
  • 29
  • 49
user7684436
  • 697
  • 14
  • 39
  • I don't get the part with _when my code adds text to the textfield in that the text doesn't get checked_ . Can you explain more in detail or add an example? – ronatory Mar 27 '17 at 19:26
  • Ok I think I understand what you mean. Saw your comment on the other answer – ronatory Mar 27 '17 at 19:34

3 Answers3

2

The way you can handle this is by implementing the UITextViewDelegate protocol in your viewcontroller. In your viewDidLoad you would want to set the delegate of your UITextField to self.

Then, simply implement the following method, like demonstrated here:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if string.length > 1 {
        // Text was pasted into the text field

        // do something with pasted text
    } else {
        //typed string
    }
    return true
}
Community
  • 1
  • 1
Forest Kunecke
  • 2,160
  • 15
  • 32
  • I have this in place. I'm looking to detect when the code adds something to the textField. Updated my question for better clarity. – user7684436 Mar 27 '17 at 19:18
0

You will want to conform to the UITextFieldDelegate protocol.

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let isAddingCharacter: Bool = range.location >= 0 && range.length == 0
    let isDeletingCharacter: Bool = range.location >= 0 && range.length == 1
    var newCount: Int = textView.text.characters.count
    if isAddingCharacter {
        newCount += 1
    } else if isDeletingCharacter {
        newCount -= 1
    }
    // If the newCount is > 0, the user is entering text
    return true
}

Side note, your outlet should be named numberOutlet, not NumberOutlet. It is convention to use camel case syntax for variable names in swift.

Chandler De Angelis
  • 2,646
  • 6
  • 32
  • 45
0

The only way I know would be just to call the method you used as selector after you add text via code.

For example you have a method which is executed after you press a button and there you add text to your textfield and the UIControlEvent doesn't get fired here. So just call the method after adding text via code in this example after pressing a button:

@IBAction func buttonPressed(_ sender: UIButton) {
  // Note: camel case convention
  numberOutlet.text?.append("added text via code")
  // perform your check method
  handleNumberImage()
}
ronatory
  • 7,156
  • 4
  • 29
  • 49
  • I did try this beforehand. The problem with this is that shouldChangeCharactersIn doesn't kick in when adding. – user7684436 Mar 28 '17 at 08:45
  • OK, so you want be able to execute your check method and `textField(_:shouldChangeCharactersIn:replacementString:)`? Because I understood that you only want to execute your check method after adding text via code. Please update your question with an example and all infos what you exactly want to do. This is probably also helpful for others – ronatory Mar 28 '17 at 11:04