Protocol

UITextFieldDelegate

A set of optional methods that you use to manage the editing and validation of text in a text field object.

Declaration

@protocol UITextFieldDelegate

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 UIKeyboardWillShowNotification and UIKeyboardDidShowNotification notifications as needed. If the keyboard or another input view was already visible, the system posts the UIKeyboardWillChangeFrameNotification and UIKeyboardDidChangeFrameNotification notifications instead.

  3. The text field calls its delegate’s textFieldDidBeginEditing: method and posts a UITextFieldTextDidBeginEditingNotification 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 UIKeyboardWillHideNotification and UIKeyboardDidHideNotification notifications.

  7. The text field calls its delegate’s textFieldDidEndEditing: method and posts a UITextFieldTextDidEndEditingNotification notification.

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

Topics

Managing Editing

- textFieldShouldBeginEditing:

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

- textFieldDidBeginEditing:

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

- textFieldShouldEndEditing:

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

- textFieldDidEndEditing:reason:

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

- textFieldDidEndEditing:

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

UITextFieldDidEndEditingReason

Constants indicating the reason why editing ended in a text field.

Editing the Text Field’s Text

- textField:shouldChangeCharactersInRange:replacementString:

Asks the delegate if the specified text should be changed.

- textFieldShouldClear:

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

- textFieldShouldReturn:

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

Relationships

Inherits From

See Also

Validating and Handling Edits

delegate

The receiver’s delegate.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software