How to get UITextField text editing changed events for user input validation - Swift Tips 1

Do you need to get the text input as the user types in a text field in your iPhone app?

You might have tried to use the UITextFieldDelegate only to find it's the wrong place to look.

You need to setup the Target/Action for the UITextField Editing Changed event (not to be confused with the Value Changed event).

Xcode Project

Download the Xcode project from the Swift Tips Course

UITextField Text Editing Changed Tutorial

1 - Make a UITextField connection from UI to code

In the Assistant Editor (Venn Diagram icon in the top right) you need to drag an Action connection from your UI to your code. You can right-click or two-finger click on the trackpad and drag.


  1. Right-click and drag from the UITextField to your ViewController.swift code file
  2. Change the dropdown from Outlet to Action.
  3. Choose Editing Changed
  4. Type a method name: textFieldEditingDidChange

2 - Connect Outlets for the text field

Drag over connections for the UITextField, and you can use the textField.text property to get it’s value.

@IBOutlet weak var button: UIButton!
@IBOutlet weak var textField: UITextField!

override func viewDidLoad() {
    // disable button
    button.enabled = false

3 - Validate the input as the user types in the UITextField

You can print out the text as the user types it for testing. 

Your app logic should do some kind of validation, which you can refactor into it’s own method called validatePassword().

As a simple example, just enable or disable the button based on your validation logic, and you can make sure it works.

@IBAction func textFieldEditingDidChange(sender: AnyObject) {
    println("textField: \(textField.text)")
    if validatePassword(textField.text) {
        // correct password
        button.enabled = true
    } else {
        button.enabled = false

func validatePassword(text: String) -> Bool {
    var result = false
    // test password
    if text == "Secret!" {
        result = true
    return result

Super Easy iPhone apps

If you want to learn how to make apps faster, using the easy method. Signup for the Pro Plan on