<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "tvOS: -",
    "visionOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UITextFieldDelegate",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(pl)UITextFieldDelegate"
  },
  "title" : "UITextFieldDelegate"
}
-->

# UITextFieldDelegate

A set of optional methods to manage editing and validating text in a text field object.

```
@MainActor protocol UITextFieldDelegate : NSObjectProtocol
```

## Overview

A text field calls the methods of its delegate in response to important changes. You use these methods to validate text that was typed by the user, to respond to specific interactions with the keyboard, and to control the overall editing process. Editing begins shortly before the text field becomes the first responder and displays the keyboard (or its assigned input view). The flow of the editing process is as follows:

1. Before becoming the first responder, the text field calls its delegate’s [`textFieldShouldBeginEditing(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldShouldBeginEditing(_:)) method. Use that method to allow or prevent the editing of the text field’s contents.
2. The text field becomes the first responder.

In response, the system displays the keyboard (or the text field’s input view) and posts the [`keyboardWillShowNotification`](/documentation/UIKit/UIResponder/keyboardWillShowNotification) and [`keyboardDidShowNotification`](/documentation/UIKit/UIResponder/keyboardDidShowNotification) notifications as needed. If the keyboard or another input view was already visible, the system posts the [`keyboardWillChangeFrameNotification`](/documentation/UIKit/UIResponder/keyboardWillChangeFrameNotification) and [`keyboardDidChangeFrameNotification`](/documentation/UIKit/UIResponder/keyboardDidChangeFrameNotification) notifications instead.
3. The text field calls its delegate’s [`textFieldDidBeginEditing(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldDidBeginEditing(_:)) method and posts a [`textDidBeginEditingNotification`](/documentation/UIKit/UITextField/textDidBeginEditingNotification) notification.
4. The text field calls various delegate methods during editing:

- Whenever the current text changes, it calls the [`textField(_:shouldChangeCharactersIn:replacementString:)`](/documentation/UIKit/UITextFieldDelegate/textField(_:shouldChangeCharactersIn:replacementString:)) method and posts the [`textDidChangeNotification`](/documentation/UIKit/UITextField/textDidChangeNotification) notification.
- It calls the [`textFieldShouldClear(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldShouldClear(_:)) method when the user taps the built-in button to clear the text.
- It calls the [`textFieldShouldReturn(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldShouldReturn(_:)) method when the user taps the keyboard’s return button.

1. Before resigning as first responder, the text field calls its delegate’s [`textFieldShouldEndEditing(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldShouldEndEditing(_:)) method. Use that method to validate the current text.
2. The text field resigns as first responder.

In response, the system hides or adjusts the keyboard as needed. When hiding the keyboard, the system posts the [`keyboardWillHideNotification`](/documentation/UIKit/UIResponder/keyboardWillHideNotification) and [`keyboardDidHideNotification`](/documentation/UIKit/UIResponder/keyboardDidHideNotification) notifications.
7. The text field calls its delegate’s [`textFieldDidEndEditing(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldDidEndEditing(_:)) method and posts a [`textDidEndEditingNotification`](/documentation/UIKit/UITextField/textDidEndEditingNotification) notification.

For more information about the features of a text field, see [`UITextField`](/documentation/UIKit/UITextField).

## Topics

### Managing editing

[`textFieldShouldBeginEditing(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldShouldBeginEditing(_:))

Asks the delegate whether to begin editing in the specified text field.

[`textFieldDidBeginEditing(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldDidBeginEditing(_:))

Tells the delegate when editing begins in the specified text field.

[`textFieldShouldEndEditing(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldShouldEndEditing(_:))

Asks the delegate whether to stop editing in the specified text field.

[`textFieldDidEndEditing(_:reason:)`](/documentation/UIKit/UITextFieldDelegate/textFieldDidEndEditing(_:reason:))

Tells the delegate when editing stops for the specified text field, and the reason it stopped.

[`textFieldDidEndEditing(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldDidEndEditing(_:))

Tells the delegate when editing stops for the specified text field.

[`UITextField.DidEndEditingReason`](/documentation/UIKit/UITextField/DidEndEditingReason)

Constants that indicate the reason for ending editing in a text field.

### Editing the text field’s text

[`textField(_:shouldChangeCharactersIn:replacementString:)`](/documentation/UIKit/UITextFieldDelegate/textField(_:shouldChangeCharactersIn:replacementString:))

Asks the delegate whether to change the specified text.

[`textFieldShouldClear(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldShouldClear(_:))

Asks the delegate whether to remove the text field’s current contents.

[`textFieldShouldReturn(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldShouldReturn(_:))

Asks the delegate whether to process the pressing of the Return button for the text field.

### Managing text selection

[`textFieldDidChangeSelection(_:)`](/documentation/UIKit/UITextFieldDelegate/textFieldDidChangeSelection(_:))

Tells the delegate when the text selection changes in the specified text field.

### Providing a context menu

[`textField(_:editMenuForCharactersIn:suggestedActions:)`](/documentation/UIKit/UITextFieldDelegate/textField(_:editMenuForCharactersIn:suggestedActions:))

Asks the delegate for the menu to display in the text field, based on the text range and actions the system provides.

### Customizing an edit menu

[`textField(_:willPresentEditMenuWith:)`](/documentation/UIKit/UITextFieldDelegate/textField(_:willPresentEditMenuWith:))

Tells the delegate that the system is about to present an edit menu with an animator.

[`textField(_:willDismissEditMenuWith:)`](/documentation/UIKit/UITextFieldDelegate/textField(_:willDismissEditMenuWith:))

Tells the delegate that the system is about to dismiss an edit menu with an animator.

### Inserting a Smart Reply suggestion

[`textField(_:insertInputSuggestion:)`](/documentation/UIKit/UITextFieldDelegate/textField(_:insertInputSuggestion:))

Tells the delegate when the keyboard delivers an input suggestion.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)