How to check if UITextField text is empty when done editing

13,594

Solution 1

I think you don't need to handle "default text" yourself. Check placeholder property of UITextField class.

Update

So placeholder is not good for your case. Did you try to implement UITextFieldDelegate methods for your UITextField and change the text in textViewDidEndEditing:?

To do so, implement UITextFieldDelegate methods those look useful for your scenario in your view controller, and set delegate of your UITextField to the view controller (either by Interface Builder or programmatically). Set the default text in textViewDidEndEditing: method.

You can refer to the "Getting the Entered Text and Setting Text" section of Text, Web, and Editing Programming Guide for iOS.

Solution 2

you have to use the textfield delegate

-(void)textFieldDidEndEditing:(UITextField *)textField;

in this delegate make check like this

if ( [textField.text length] == 0 )
{
    // the text field is empty do your work
} else {
    ///  the text field is not empty 
}

Solution 3

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
    if (range.location == 0 && string.length == 0)
    {
        //textField is empty
    }
    return YES;
}

Solution 4

When you get the delegate call for when the text field returns, use the sender parameter to check the text (sender.text), and if it is equal to @"" set your default text.

Solution 5

I use 2 approaches. My choice depends on the application business logic.

1) I check the resulting text after applying changes in shouldChangeCharactersIn:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        var text = textField.text ?? ""

        let startIndex = text.index(text.startIndex, offsetBy: range.location)
        let endIndex = text.index(text.startIndex, offsetBy: range.location + range.length)
        text = text.replacingCharacters(in: startIndex..<endIndex, with: string)
        if text.count == 0 {
            // textField is empty
        } else {
            // textField is not empty
        }
        return true
    }

2) I use editingChanged action:

@IBAction private func textFieldDidChange(_ sender: UITextField) {
        if (sender.text?.count ?? 0) == 0 {
            // textField is empty
        } else {
            // textField is not empty
        }
    }
Share:
13,594
starscream_disco_party
Author by

starscream_disco_party

Updated on June 05, 2022

Comments

  • starscream_disco_party
    starscream_disco_party almost 2 years

    I currently have a UITextField with default text and set to clear when editing begins. I'm trying to make it so that if the field is empty or nil when editing is done the text value will be the default text again. I'm having trouble detecting when either editing is done or the keyboard is dismissed. Could anyone tell me which detection method I would want to use and how to implement it? Thanks so much!


    Edit


    A placeholder can't be used in my case