Protocol

UITextFieldDelegate

The UITextFieldDelegate protocol defines methods that you use to manage the editing and validation of text in a UITextField object. All of the methods of this protocol are optional.

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 UIKeyboardWillShow and UIKeyboardDidShow notifications as needed. If the keyboard or another input view was already visible, the system posts the UIKeyboardWillChangeFrame and UIKeyboardDidChangeFrame notifications instead.

  3. The text field calls its delegate’s textFieldDidBeginEditing(_:) method and posts a UITextFieldTextDidBeginEditing notification.

  4. The text field calls various delegate methods during editing:

  5. Before resigning as first responder, the text field calls its delegate’s textFieldShouldEndEditing(_:) method. Use that method to validate the current text.

  6. 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 UIKeyboardWillHide and UIKeyboardDidHide notifications.

  7. The text field calls its delegate’s textFieldDidEndEditing(_:) method and posts a UITextFieldTextDidEndEditing notification.

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

Symbols

Managing Editing

func textFieldShouldBeginEditing(UITextField)

Asks the delegate if editing should begin in the specified text field.

func textFieldDidBeginEditing(UITextField)

Tells the delegate that editing began in the specified text field.

func textFieldShouldEndEditing(UITextField)

Asks the delegate if editing should stop in the specified text field.

func textFieldDidEndEditing(UITextField)

Tells the delegate that editing stopped for the specified text field.

Editing the Text Field’s Text

func textFieldShouldClear(UITextField)

Asks the delegate if the text field’s current contents should be removed.

func textFieldShouldReturn(UITextField)

Asks the delegate if the text field should process the pressing of the return button.

Relationships

Inherits From