iOS Developer Library

Developer

UIKit Framework Reference UITextFieldDelegate Protocol Reference

Options
Deployment Target:

On This Page
Language:

UITextFieldDelegate

Inherits From


Not Applicable

Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 2.0 and later

The UITextFieldDelegate protocol defines the messages sent to a text field delegate as part of the sequence of editing its text. All of the methods of this protocol are optional.

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

    Declaration

    Swift

    optional func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool

    Objective-C

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

    Parameters

    textField

    The text field for which editing is about to begin.

    Return Value

    YEStrue if an editing session should be initiated; otherwise, NOfalse to disallow editing.

    Discussion

    When the user performs an action that would normally initiate an editing session, the text field calls this method first to see if editing should actually proceed. In most circumstances, you would simply return YEStrue from this method to allow editing to proceed.

    Implementation of this method by the delegate is optional. If it is not present, editing proceeds as if this method had returned YEStrue.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • Tells the delegate that editing began for the specified text field.

    Declaration

    Swift

    optional func textFieldDidBeginEditing(_ textField: UITextField)

    Objective-C

    - (void)textFieldDidBeginEditing:(UITextField *)textField

    Parameters

    textField

    The text field for which an editing session began.

    Discussion

    This method notifies the delegate that the specified text field just became the first responder. You can use this method to update your delegate’s state information. For example, you might use this method to show overlay views that should be visible while editing.

    Implementation of this method by the delegate is optional.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

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

    Declaration

    Swift

    optional func textFieldShouldEndEditing(_ textField: UITextField) -> Bool

    Objective-C

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField

    Parameters

    textField

    The text field for which editing is about to end.

    Return Value

    YEStrue if editing should stop; otherwise, NOfalse if the editing session should continue

    Discussion

    This method is called when the text field is asked to resign the first responder status. This might occur when your application asks the text field to resign focus or when the user tries to change the editing focus to another control. Before the focus actually changes, however, the text field calls this method to give your delegate a chance to decide whether it should.

    Normally, you would return YEStrue from this method to allow the text field to resign the first responder status. You might return NOfalse, however, in cases where your delegate detects invalid contents in the text field. By returning NOfalse, you could prevent the user from switching to another control until the text field contained a valid value.

    Be aware that this method provides only a recommendation about whether editing should end. Even if you return NOfalse from this method, it is possible that editing might still end. For example, this might happen when the text field is forced to resign the first responder status by being removed from its parent view or window.

    Implementation of this method by the delegate is optional. If it is not present, the first responder status is resigned as if this method had returned YEStrue.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

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

    Declaration

    Swift

    optional func textFieldDidEndEditing(_ textField: UITextField)

    Objective-C

    - (void)textFieldDidEndEditing:(UITextField *)textField

    Parameters

    textField

    The text field for which editing ended.

    Discussion

    This method is called after the text field resigns its first responder status. You can use this method to update your delegate’s state information. For example, you might use this method to hide overlay views that should be visible only while editing.

    Implementation of this method by the delegate is optional.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • Asks the delegate if the specified text should be changed.

    Declaration

    Swift

    optional func textField(_ textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool

    Objective-C

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

    Parameters

    textField

    The text field containing the text.

    range

    The range of characters to be replaced

    string

    The replacement string.

    Return Value

    YEStrue if the specified text range should be replaced; otherwise, NOfalse to keep the old text.

    Discussion

    The text field calls this method whenever the user types a new character in the text field or deletes an existing character.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

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

    Declaration

    Swift

    optional func textFieldShouldClear(_ textField: UITextField) -> Bool

    Objective-C

    - (BOOL)textFieldShouldClear:(UITextField *)textField

    Parameters

    textField

    The text field containing the text.

    Return Value

    YEStrue if the text field’s contents should be cleared; otherwise, NOfalse.

    Discussion

    The text field calls this method in response to the user pressing the built-in clear button. (This button is not shown by default but can be enabled by changing the value in the clearButtonMode property of the text field.) This method is also called when editing begins and the clearsOnBeginEditing property of the text field is set to YEStrue.

    Implementation of this method by the delegate is optional. If it is not present, the text is cleared as if this method had returned YEStrue.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

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

    Declaration

    Swift

    optional func textFieldShouldReturn(_ textField: UITextField) -> Bool

    Objective-C

    - (BOOL)textFieldShouldReturn:(UITextField *)textField

    Parameters

    textField

    The text field whose return button was pressed.

    Return Value

    YEStrue if the text field should implement its default behavior for the return button; otherwise, NOfalse.

    Discussion

    The text field calls this method whenever the user taps the return button. You can use this method to implement any custom behavior when the button is tapped.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later