Protocol

UIText​Field​Delegate

The UIText​Field​Delegate protocol defines methods that you use to manage the editing and validation of text in a UIText​Field 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 text​Field​Should​Begin​Editing(_:​) 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 UIKeyboard​Will​Show and UIKeyboard​Did​Show notifications as needed. If the keyboard or another input view was already visible, the system posts the UIKeyboard​Will​Change​Frame and UIKeyboard​Did​Change​Frame notifications instead.

  3. The text field calls its delegate’s text​Field​Did​Begin​Editing(_:​) method and posts a UIText​Field​Text​Did​Begin​Editing notification.

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

  5. Before resigning as first responder, the text field calls its delegate’s text​Field​Should​End​Editing(_:​) 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 UIKeyboard​Will​Hide and UIKeyboard​Did​Hide notifications.

  7. The text field calls its delegate’s text​Field​Did​End​Editing(_:​) method and posts a UIText​Field​Text​Did​End​Editing notification.

For more information about the features of a text field, see UIText​Field.

Symbols

Managing Editing

func text​Field​Should​Begin​Editing(UIText​Field)

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

func text​Field​Did​Begin​Editing(UIText​Field)

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

func text​Field​Should​End​Editing(UIText​Field)

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

func text​Field​Did​End​Editing(UIText​Field)

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

Editing the Text Field’s Text

func text​Field​Should​Clear(UIText​Field)

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

func text​Field​Should​Return(UIText​Field)

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

Relationships

Inherits From