Madivad
Madivad

Reputation: 3337

clearing a UITextField when the user starts typing

short version: How can I make a UITextField box remove all content on the users first keypress? I don't want the info removed until the user starts typing something. ie, clearing it on begin edit is not good enough.

long version: I have three UITextField that loop around (using the return key and catching the press in the "shouldReturn" method. There is text already in the UITextField, and if the user doesn't type anything and just goes to the next UITextField, the value should stay (default behaviour).

But I want it that if the user starts typing, it automatically clears the text first. Something like having the whole field highlighted, and then typing anything deletes the fiels and then adds the user keypress.

"Clear when editing begins" is no good, because the text is immediately cleared on the cursor appearing in the field. That's not desired. I thought I could use the placeholder here, but that doesn't work as a default, and I can't find a default value property. The Highlighted and Selected properties don't do anything in this regard either.

Upvotes: 8

Views: 14151

Answers (7)

Vivek Deore
Vivek Deore

Reputation: 71

Declare and initialize a NSString variable for your textField's initial text

NSString *initialText=@"initial text";

Then implement methods:

    -(void)textFieldDidBeginEditing:(UITextField *)textField
 {
    if(textField.text isEqualToString:initialText)
         { 
           textField.text=@"";
        }
   }

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if(textField.text isEqualToString:@"")
    {
        textField.text=initialText;
    }
}

Upvotes: 0

Kamiccolo
Kamiccolo

Reputation: 11

If you want to clear the text one the user interacts with it, there is an option in interface builder to where you can set the text field to "Clear when editing begins."

Upvotes: 1

abbood
abbood

Reputation: 23548

Another solution that fulfils the same purpose is by simply using a text field placeholder which is defined as:

The string that is displayed when there is no other text in the text field.

So as soon as the user starts typing, the placeholder text disappears.

That's something you can set from the storyboard, or programmatically. (Yes it took me two hours trying to figure it the harder way.. when the solution was literally one line change of code).

Upvotes: 1

Madivad
Madivad

Reputation: 3337

I want to thank people for their answers, I implemented both of the main methods described here and both worked flawlessly. But I have since come across a much simpler, nicer answer and involves only one line of code :)

In the textField's didBeginEditing method, place [self.textField selectAll:self]; or [self.textField selectAll:nil];

The original answer I found had selectAll:self but this shows the cut/copy/paste menu. If you send nil instead of self the menu doesn't appear.

Adding this one line of code highlights the text on entering the textField (so gives the user a visual cue), and only removes everything once a key is pressed.

Upvotes: 2

Dave
Dave

Reputation: 1081

There is a delegate method called

textFieldDidBeginEditing:(UITextField*) tf{
  tf.startedEdinting = YES;
}

textFeildDidEndEditing: (UITextField*) tf {
  tf.startedEditing = NO;
}

Add startEditing in a category to UITextField.

Then if value changes clear the field:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
 if (textField.startEditing){
  textField.text = string;
 } else {
  textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
 }
}

You can add the property to the UITextField category in the following way:

.h

@property (nonatomic, assign) BOOL startEditing;

.m

@dynamic startEditing;

- (void) setStartEditing:(BOOL)startEditing_in{ 
NSNumber* num = [NSNumber numberWithBool:startEditing_in]; 
objc_setAssociatedObject(self, myConstant, num, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
} 
- (BOOL) startEditing{ 
NSNumber* num = objc_getAssociatedObject(self, myConstant); 
return [num boolValue]; 
}

Upvotes: 3

user1829646
user1829646

Reputation:

Try to use the following method.

- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {  
  if(isFirsttime==YES)
{
textfield.text==@"";
isFirsttime=NO;
}
return YES;
     }

Upvotes: 0

Midhun MP
Midhun MP

Reputation: 107121

Declare a BOOL variable in your .h file like.

BOOL clearField;

And implement the delegate methods like:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
   clearField = YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
  clearField = NO;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
  clearField = NO;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
  if(clearField)
  {
     textField.text = @""
     clearField = NO;
  }
}

Upvotes: 2

Related Questions