How do I check when a UITextField changes?

I am trying to check when a text field changes, equivalent too the function used for textView - textViewDidChange so far I have done this:

  func textFieldDidBeginEditing(textField: UITextField) {
        if self.status.text == "" && self.username.text == "" {
            self.topRightButton.enabled = false
        } else {   
            self.topRightButton.enabled = true
        }
    }

Which kind of works, but the topRightButton is enabled as soon as the text field is pressed on, I want it to be enabled only when text is actually typed in?

SWIFT

Swift 4.2

textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)

and

@objc func textFieldDidChange(_ textField: UITextField) {

}

SWIFT 3 & swift 4.1

textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)

and

func textFieldDidChange(_ textField: UITextField) {

}

SWIFT 2.2

textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)

and

func textFieldDidChange(textField: UITextField) {
    //your code
}

OBJECTIVE-C

[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

and textFieldDidChange method is

-(void)textFieldDidChange :(UITextField *) textField{
    //your code
}

The appearance of the keyboard has the potential to obscure portions of your user interface. You should update your interface as needed to ensure that the text field being edited is visible. Use the keyboard notifications to detect the appearance and disappearance of the keyboard and to make necessary changes to your interface.

You can make this connection in interface builder.

  1. In your storyboard, click the assistant editor at the top of the screen (two circles in the middle).

  2. Ctrl + Click on the textfield in interface builder.

  3. Drag from EditingChanged to inside your view controller class in the assistant view.

  4. Name your function ("textDidChange" for example) and click connect.

This will check a UITextField for a proper email. Add this method to the textFields delegate then check if the characters it is about to change should be added or not. Return YES or NO depending on the text fields current text compared to a valid email address:

Swift 5.0
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)),
                          for: .editingChanged)

and handle method:

@objc func textFieldDidChange(_ textField: UITextField) {

}
Swift 4.0
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)),
                          for: UIControlEvents.editingChanged)

and handle method:

@objc func textFieldDidChange(_ textField: UITextField) {

}
Swift 3.0
textField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)

and handle method:

func textFieldDidChange(textField: UITextField) { 

}

Hi, what can I do to check a textfield that's in a custom editor window? I have tried GUI.changed, but it checks everything above it, which I don't want because I have some buttons up above that GUI.changed code, which shouldn't set the bool "editingAchi" to true.

The way I've handled it so far: in UITextFieldDelegate

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    // text hasn't changed yet, you have to compute the text AFTER the edit yourself
    let updatedString = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)

    // do whatever you need with this updated string (your code)


    // always return true so that changes propagate
    return true
}

Swift4 version

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
    return true
}

I am a beginner to swift and I am trying to sync the UITextField and a UISlider. I have tried to add a listener to the slider so the text field can change whenever the slider changes and vice versa

Swift 3

 textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(sender:)), for: UIControlEvents.editingChanged)

Change UITextField and UITextView Cursor / Caret Color. 282. How do I check when a UITextField changes? 0. Change the size of UITextField inside UISearchBar.

Pro-active and in-depth file server auditing is an essential part of maintaining a secure IT environment. Organisations need to be sure that they can keep track of who is making changes to their critical files and folders and what those changes are.

Melanie Pinola is a freelance writer covering all things tech-related. A former IT admin and occasional web developer, she is also the author of LinkedIn in 30 Minutes, a Lifehacker writer, and

func textField(_ textFieldToChange: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {Let’s assume our requirement is to not allow any punctuation to be typed in to the field. We can do that using CharacterSet to check if the new string contains any punctuation.

Comments
  • This crashes for me and I don't understand why.
  • Checked multiple times. Delegate is set immediately before it inside viewDidLoad. The action is letter for letter the same. The app crashes as soon as a keyboard button is pressed. Edit: Figured it out! Was missing the semicolon inside the action. I assumed it only had to be same as the function name.
  • @FawadMasud this does nothing now in Swift 2.0 on iOS 9 with XCode 7 has it been depreciated or do you know the current way to fix it?
  • @bibscy yes you have to loop through all textfields inside a view.
  • For Swift 4.2 its: Texttfield.addTarget(self, action: #selector(ViewControllerr.textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
  • This is a great solution especially especially if dealing with a UITextField in a tableViewCell that is being managed by a separate datasource. This approach allows the viewController to respond directly (thus the datasource doesn't have to respond and delegate the action).
  • Great - a simple solution to an irritating issue. You can of course link multiple textfield
  • Probably a better answer than above because eliminating addition @objc func.
  • Good Idea , I use event DidEndEditing
  • This is the best solution. Thanks @rmooney!