iOS Development
UITextField
Swift Programming
Return Key Event
Mobile App Development

UITextField - capture return button event

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Capturing the Return key in a UITextField is a standard part of form behavior on iOS. The usual choices are the delegate callback textFieldShouldReturn or the control event .editingDidEndOnExit. Which one you use depends on whether the text field lives in a delegate-driven form or in a more target-action-oriented setup.

Use the Delegate for Standard Form Flow

The most common solution is to adopt UITextFieldDelegate and implement textFieldShouldReturn.

swift
1import UIKit
2
3final class ViewController: UIViewController, UITextFieldDelegate {
4    let textField = UITextField()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        textField.borderStyle = .roundedRect
9        textField.placeholder = "Enter text"
10        textField.returnKeyType = .done
11        textField.delegate = self
12        textField.frame = CGRect(x: 20, y: 100, width: 240, height: 40)
13        view.addSubview(textField)
14    }
15
16    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
17        print("Return pressed with text:", textField.text ?? "")
18        textField.resignFirstResponder()
19        return true
20    }
21}

This is the right pattern when the Return key should submit, dismiss the keyboard, or move to the next input field.

.editingDidEndOnExit Also Works

UITextField is a control, so it can also fire events. If you prefer target-action over delegates, attach .editingDidEndOnExit.

swift
1import UIKit
2
3final class ViewController: UIViewController {
4    let textField = UITextField()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        textField.borderStyle = .roundedRect
9        textField.frame = CGRect(x: 20, y: 100, width: 240, height: 40)
10        textField.addTarget(self, action: #selector(returnPressed(_:)), for: .editingDidEndOnExit)
11        view.addSubview(textField)
12    }
13
14    @objc private func returnPressed(_ sender: UITextField) {
15        print("Return pressed with text:", sender.text ?? "")
16        sender.resignFirstResponder()
17    }
18}

This is especially useful when the view controller is not already acting as the text field delegate or when you want a lighter event-handling setup.

Move Between Fields Intentionally

Many apps use Return to jump to the next field instead of dismissing the keyboard immediately. In that case, textFieldShouldReturn is often clearer because it can branch on which field triggered the event.

swift
1func textFieldShouldReturn(_ textField: UITextField) -> Bool {
2    if textField == firstNameField {
3        lastNameField.becomeFirstResponder()
4    } else {
5        textField.resignFirstResponder()
6    }
7    return true
8}

That gives one place to manage form flow rather than scattering navigation behavior across unrelated callbacks.

Set the Return Key Type to Match the Action

The Return key can say Done, Next, Search, Go, and more. The label does not change the callback mechanism, but it does change user expectations.

If the field searches, use .search. If it advances through a form, use .next. If it finishes editing, use .done. Clear keyboard intent makes the interface feel more coherent.

Users notice that coherence immediately even if they never name it explicitly. The label on the key sets an expectation about what will happen when they press it.

Do Not Confuse Return with General Editing Changes

Pressing Return is not the same as editing text. Methods that observe text changes fire while the user types. The Return event is about submission or field exit.

That distinction matters because developers sometimes wire up text-change observers and then wonder why Return-specific behavior still feels awkward. Use the callback that matches the interaction you actually care about.

Common Pitfalls

  • Forgetting to assign the text field delegate when relying on textFieldShouldReturn.
  • Using a text-change callback when the requirement is specifically the Return key.
  • Returning false unintentionally and then wondering why the text field does not behave as expected.
  • Not dismissing or redirecting first responder status after the event.
  • Using the wrong returnKeyType, which makes the keyboard hint misleading.

Summary

  • Use textFieldShouldReturn for the standard delegate-based solution.
  • Use .editingDidEndOnExit when target-action fits better.
  • Resign or move first responder status intentionally after Return is pressed.
  • Match returnKeyType to the real action.
  • Separate Return handling from general text-change handling.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.