Contents

UITextFieldDelegate

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

Declaration

@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(_:) 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 and keyboardDidShowNotification notifications as needed. If the keyboard or another input view was already visible, the system posts the keyboardWillChangeFrameNotification and keyboardDidChangeFrameNotification notifications instead. 3. The text field calls its delegate’s textFieldDidBeginEditing(_:) method and posts a textDidBeginEditingNotification notification. 4. The text field calls various delegate methods during editing:

  1. Before resigning as first responder, the text field calls its delegate’s 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 and keyboardDidHideNotification notifications. 7. The text field calls its delegate’s textFieldDidEndEditing(_:) method and posts a textDidEndEditingNotification notification.

For more information about the features of a text field, see UITextField.

Topics

Managing editing

Editing the text field’s text

Managing text selection

Providing a context menu

Customizing an edit menu

Inserting a Smart Reply suggestion

Instance Methods

See Also

Validating and handling edits