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!"); }
}