How to check if UITextField text is empty when editing

I currently have a UITextField with default text and is set to clear when editing starts. I am trying to make it so that if the field is empty or zero when editing is done, the text value will again become the default text. I am having trouble detecting when either editing is done or the keyboard is rejected. Can someone tell me which detection method I would like to use and how to implement it? Thank!


Edit


A placeholdercannot be used in my case

+3
source share
4 answers

, " " . placeholder UITextField.

, placeholder . UITextFieldDelegate UITextField textViewDidEndEditing:?

, UITextFieldDelegate, , delegate UITextField ( Interface Builder, ). textViewDidEndEditing:.

" " , - iOS.

+4

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

,

if ( [textField.text length] == 0 )
{
    // the text field is empty do your work
} else {
    ///  the text field is not empty 
}
+13
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
    if (range.location == 0 && string.length == 0)
    {
        //textField is empty
    }
    return YES;
}
+6

When you receive a delegate call when a text field is returned, use the parameter senderto check the text ( sender.text), and if it is equal @"", set the default text.

+2
source

All Articles