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) 
2
  • 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? Commented Mar 27, 2017 at 19:26
  • Ok I think I understand what you mean. Saw your comment on the other answer Commented Mar 27, 2017 at 19:34

3 Answers 3

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 } 
Sign up to request clarification or add additional context in comments.

1 Comment

I have this in place. I'm looking to detect when the code adds something to the textField. Updated my question for better clarity.
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.

Comments

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() } 

2 Comments

I did try this beforehand. The problem with this is that shouldChangeCharactersIn doesn't kick in when adding.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.