7

How can I make a text field box remove all content on the user's first keypress?

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if([tfieldDOB.text length] == 4) { tfieldDOB.text=[NSString stringWithFormat:@"%@/",tfieldDOB.text]; } else if([tfieldDOB.text length]==7) { tfieldDOB.text=[NSString stringWithFormat:@"%@/",tfieldDOB.text]; } return YES; } 
1
  • after typing we can't clear it.@pe60t0 Commented Apr 18, 2014 at 10:08

2 Answers 2

12

change the textfield attribute clear button mode in appears while editing

or other choice just use the single line, where you need to add

yourtextfieldname.text=@""; //it is used for clear the textfield values 

Swift

yourtextfieldname.text="" 

or another way

 clearField =@"YES"; if([clearField isequaltostring:@"YES"]) //check this line in { tfieldDOB.text = @""; clearField =@"NO"; } 
Sign up to request clarification or add additional context in comments.

4 Comments

suppose i type 1990/11/11,and when i want to edit it then only last two digit edited ..i.e not completely edit..@anbu
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if([tfieldDOB.text length] == 4) { tfieldDOB.text=[NSString stringWithFormat:@"%@/",tfieldDOB.text]; } else if([tfieldDOB.text length]==7) { tfieldDOB.text=[NSString stringWithFormat:@"%@/",tfieldDOB.text]; } return YES; }
k where is your clearfield condition, where declare the clearfield value,
first of all tfieldDOB is empty,now i've typed my date of birth 1990/11/01,but suppose i want to edit it then i can edit only (1990/11/)..@anbu
5

Implement the text field's delegate method textFieldShouldBeginEditing: and set the text as empty string when the text field is just about to being editing.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ [textField setText:@""]; return YES; } 

Or you can set the property clearsOnBeginEditing of the textfield as

[textField setClearsOnBeginEditing:YES]; 

and it will clear the text when editing begins

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.