Learn to edit the end of a UITextfield without resigning the first responder

When the user enters UITextfield and it stops for 2 seconds, the cursor is still on UITextfield , so how can we identify this event? that is, I want to check whether editing has finished or not, without canceling the first responder from this UITextfield . What is the way to do this?

+4
source share
4 answers

Yes, we can check it out! with delegate UITextField , - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

 - (void) callMeAfterTwoSeconds { NSLog(@"I'll call after two seconds of inactivity!"); } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { [NSRunLoop cancelPreviousPerformRequestsWithTarget:self]; [self performSelector:@selector(callMeAfterTwoSeconds) withObject:nil afterDelay:2.0]; return YES; } 

While you are typing (by pressing the keys on the keyboard), it cancels previous calls to the callMeAfterTwoSeconds function, after you stop, it makes it ring after 2 seconds of delay, and yes, it will ring after 2 seconds.

Update: Even you can pass this text field as an object to performSelector to find out which text field is inactive, what your callMeAfterTwoSeconds function will be callMeAfterTwoSeconds ,

 - (void) callMeAfterTwoSeconds:(UITextField *)textfield { if(textfield == txtUserName) { NSLog(@"User textfield has NO activity from last two seconds!"); } } 
+10
source

Go to the connection inspector of your UITextField and connect the β€œEdited UITextField ” from the β€œSent Events” list to the predefined IBAction of your choice. Alternatively, you can do this programmatically if you are not working with the Storyboard.

 [youTextField addTarget:self action:@selector(textFieldInputDidChange:) forControlEvents:UIControlEventEditingChanged]; 

Now the IBAction that you just connected will be launched every time the user changes the character in the UITextField . Create a timer as ivar. Now every time IBAction is called, start the timer if it strikes for 2 seconds without restarting with a new call, which, as you know, the user did not enter / delete values ​​in the UITextField .

+6
source

I did this for searchBar, but I think it works for UITextField as well. The code is in Swift. :)

 func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { searchTimer?.invalidate() searchTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("search"), userInfo: nil, repeats: false) } func search() { println("search \(searchBar.text)") } 
+2
source

I don't think calling [NSRunLoop cancelPreviousPerformRequestsWithTarget:self] is good practice.

Instead, I would do:

 [self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; //or select the textfield in the storyboard, go to connections inspector and choose the target for 'Editing Changed' - (IBAction)textFieldDidChange:(id)sender { [self performSelector:@selector(editingChanged:) withObject:self.searchTextField.text afterDelay:2.0]; } - (void)editingChanged:(NSString *)text { if ([text isEqualToString:self.searchTextField.text]) { //do your thing } } 

in this way the user can print, and he will call the editingChanged: call, then you can double-check whether the value has changed, and if this does not happen, the user will stop printing for 2 seconds.

-1
source

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


All Articles