Clearing UITextField when user starts typing

short version . How can I make the UITextField field delete the entire contents of the first click of a button? I do not want the information deleted until the user starts typing something. those. clearing it at the start of editing is not enough.

long version . I have three UITextField that go around (using the return key and catches the press in the “shouldReturn” method. There is text in the UITextField, and if the user does not do this, enter something and just go to the next UITextField, the value should remain ( default behavior).

But I want that if the user starts typing, he first automatically clears the text. Something like that the entire field is selected, and then it types something, deletes the fields, and then adds a custom click.

“Clear when editing starts” is not good, because the text immediately clears the cursor that appears in the field. This is undesirable. I thought I could use the placeholder here, but this does not work as the default, and I cannot find the default property. Selected and selected properties do nothing in this regard.

+4
source share
7 answers

Declare a BOOL variable in your .h file, for example.

 BOOL clearField; 

And implement the delegate methods, for example:

 -(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; } } 
+1
source

There is a delegate method called

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

Add startEditing to categories in UITextField.

Then, if the value changes 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 a property to the UITextField category as follows:

.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]; } 
+2
source

I want to thank people for their answers, I implemented both of the basic methods described here, and both worked flawlessly. But since then I came across a much simpler and more pleasant answer and includes only one line of code :)

In the textField didBeginEditing method put [self.textField selectAll:self]; or [self.textField selectAll:nil];

The original answer I found was selectAll:self , but this shows the cut / copy / paste menu. If you send nil instead of self , the menu is not displayed.

Adding this one line of code underlines the text when entering a text field (this gives the user a visual hint) and only deletes everything as soon as the key is pressed.

+1
source

Another solution that performs the same task is a simple placeholder text field, 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 will disappear.

This is what you can install from the storyboard, or programmatically . (Yes, it took me two hours trying to figure it out more difficult ... when the solution was literally one change of code).

+1
source

If you want to clear the text that the user interacts with, there is an option in the interface designer where you can set the "Clear when editing starts" text box.

+1
source

Try the following method.

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

Declare and initialize the NSString variable for textField source

 NSString * initialText=@ "initial text"; 

Then we implement the methods:

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

Source: https://habr.com/ru/post/1446457/


All Articles