starscream_disco_party
starscream_disco_party

Reputation: 2996

How to check if UITextField text is empty when done editing

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

Upvotes: 3

Views: 14180

Answers (6)

Valentin Shamardin
Valentin Shamardin

Reputation: 3658

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
        }
    }

Upvotes: 2

Alexis Candelaria
Alexis Candelaria

Reputation: 257

You also need to check wether the deleted range is the entire length of the text.

func textField(_ textField: UITextField, shouldChangeCharactersIn range:  NSRange, replacementString string: String) -> Bool {
    if !(range.location == 0 && string.count == 0 && range.length == textField.text?.count) {
      // Text field is empty
    }
    return true
}

Upvotes: 1

Omar Freewan
Omar Freewan

Reputation: 2678

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 
}

Upvotes: 13

Valentin Shamardin
Valentin Shamardin

Reputation: 3658

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

Upvotes: 9

David Skrundz
David Skrundz

Reputation: 13347

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.

Upvotes: 2

Hailei
Hailei

Reputation: 42163

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.

Upvotes: 4

Related Questions