iOS
UITextField
Swift
return key
user interface

how to add an action on UITextField return key?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

On iOS, pressing the return key in a UITextField usually means one of three things: submit the value, move to the next field, or dismiss the keyboard. The standard way to handle that action is through the UITextFieldDelegate, though target-action also works for simpler cases.

The Delegate Method You Usually Want

UITextFieldDelegate includes textFieldShouldReturn(_:), which is called when the user taps the return key.

swift
1import UIKit
2
3final class LoginViewController: UIViewController, UITextFieldDelegate {
4    private let emailField = UITextField()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        view.backgroundColor = .systemBackground
9
10        emailField.borderStyle = .roundedRect
11        emailField.placeholder = "Email"
12        emailField.returnKeyType = .done
13        emailField.delegate = self
14    }
15
16    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
17        print("Return key pressed")
18        textField.resignFirstResponder()
19        return true
20    }
21}

Returning true allows the text field to process the return normally. Calling resignFirstResponder() dismisses the keyboard.

Moving to the Next Field

In forms, the return key often advances the user to another input rather than closing the keyboard.

swift
1import UIKit
2
3final class SignupViewController: UIViewController, UITextFieldDelegate {
4    let firstNameField = UITextField()
5    let lastNameField = UITextField()
6
7    override func viewDidLoad() {
8        super.viewDidLoad()
9
10        firstNameField.returnKeyType = .next
11        lastNameField.returnKeyType = .done
12
13        firstNameField.delegate = self
14        lastNameField.delegate = self
15    }
16
17    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
18        if textField === firstNameField {
19            lastNameField.becomeFirstResponder()
20        } else {
21            textField.resignFirstResponder()
22        }
23        return true
24    }
25}

This pattern is common because it makes the keyboard behave like a natural part of the form flow.

Triggering a Submit Action

If the return key should submit the form, call the same method your button would use.

swift
1func textFieldShouldReturn(_ textField: UITextField) -> Bool {
2    submitLogin()
3    return true
4}
5
6private func submitLogin() {
7    print("Submit form")
8}

This keeps the form logic in one place instead of duplicating it between the button and the return-key path.

Using Target-Action Instead

For simpler cases, you can respond to .editingDidEndOnExit, which fires when the return key ends editing.

swift
1import UIKit
2
3final class DemoViewController: UIViewController {
4    let textField = UITextField()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8
9        textField.addTarget(
10            self,
11            action: #selector(returnPressed),
12            for: .editingDidEndOnExit
13        )
14    }
15
16    @objc private func returnPressed() {
17        print("Return key pressed")
18    }
19}

This is fine for one field, but the delegate approach scales better when you have several text fields or more nuanced behavior.

Configure the Return Key Type

The label on the key should match the action you expect.

swift
textField.returnKeyType = .search

Useful return key types include:

  • '.done'
  • '.next'
  • '.go'
  • '.search'
  • '.send'

This does not change the logic automatically, but it gives the user a better cue.

A Practical Form Pattern

In a real form, you usually combine three behaviors:

  • assign the delegate
  • set a sensible return key type
  • move focus or submit based on which field is active

That creates predictable keyboard behavior without extra gesture code or button-only flows.

Common Pitfalls

The biggest pitfall is forgetting to set the text field's delegate. If the delegate is not assigned, textFieldShouldReturn(_:) will never be called.

Another issue is dismissing the keyboard without handling the actual intended action. If the return key is supposed to submit or advance focus, make that explicit.

Developers also duplicate form-submission logic inside both the delegate method and a button action. It is cleaner to route both through one shared method.

Finally, make sure the chosen returnKeyType matches the action. A key labeled .done that silently moves to the next field is a poor UI signal.

Summary

  • Use textFieldShouldReturn(_:) for the standard return-key action.
  • Assign the text field's delegate or the method will not fire.
  • Use the return key to dismiss the keyboard, move to the next field, or submit the form.
  • 'editingDidEndOnExit is a valid target-action alternative for simpler cases.'
  • Match returnKeyType to the actual user action for better UX.

Course illustration
Course illustration

All Rights Reserved.