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!
A placeholdercannot be used in my case
placeholder
, " " . placeholder UITextField.
UITextField
, placeholder . UITextFieldDelegate UITextField textViewDidEndEditing:?
UITextFieldDelegate
textViewDidEndEditing:
, UITextFieldDelegate, , delegate UITextField ( Interface Builder, ). textViewDidEndEditing:.
delegate
" " , - iOS.
-(void)textFieldDidEndEditing:(UITextField *)textField;
,
if ( [textField.text length] == 0 ) { // the text field is empty do your work } else { /// the text field is not empty }
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string { if (range.location == 0 && string.length == 0) { //textField is empty } return YES; }
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.
sender
sender.text
@""