When user is typing in UITextfield
, and he stops for 2 seconds, the cursor is still on UITextfield
, so how we can identify this event? i.e. I want to check the whether the editing is end or not without resigning the first responser from that UITextField
.
What is the way to do that?
Yes, we can check that! with UITextField
delegate, - (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're typing (tapping keys from keyboard), it will cancel previous calls for the callMeAfterTwoSeconds
function, once you stop, it sets it to call after 2 seconds delay, and yes, it will call after 2 seconds.
Update:
Even you can pass that textfield as object to performSelector
to know which textfield is inactive, for that your callMeAfterTwoSeconds
function will be like,
- (void) callMeAfterTwoSeconds:(UITextField *)textfield {
if(textfield == txtUserName) {
NSLog(@"User textfield has NO activity from last two seconds!"); }
}
Go to the connections inspector of your UITextField
and connect the "Editing Changed" from the Sent Events list to a predefined IBAction
of your choice. Alternatively you can do it programmatically if you are not working with Storyboard.
[youTextField addTarget:self action:@selector(textFieldInputDidChange:) forControlEvents:UIControlEventEditingChanged];
Now the IBAction
you've just connected will be fired every time the user changes a character in the UITextField
. Create a timer as ivar. Now every time IBAction
gets called, start the timer, if it will hit 2sec without being restarted by a new call you know the user hasn't entered/deleted values in the UITextField
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With