views:

31

answers:

4

Hi,

I am wondering if there is a way to tell if the user hasn't typed in the UITextField say for 2 seconds.

If that's not possible, I'd like to know if there is a way to tell if the user made an error typing in the text field - example:

There is a UITextView which contains objects from an NSArray and the user has to type in the textfield and match the content on the text view. If they make a mistake, it will tell them (what I need help with) - Note: if the user types the string correctly, it automatically accepts it and goes on to the next one - there is no button to submit the string - it does that via the text field.

Thanks!

+1  A: 

This is possible, but it is going to involve some work. You will need to check out the NSTimer and NSDate classes and the UITextFieldDelegate protocol. Basically, every time the user enters something into the text field, you get a delegate method to fire. That method then caches the current time (via [NSDate date]) and starts a timer with a two-second duration. When the timer fires, you have it trigger a method that checks the cached NSDate to see if the last input into the text field was within the last two seconds.

William Jockusch
Hmmm thanks. Do you know of any other way I could achieve this? (tell if the user types it incorrectly)
Alex
If you want to go the timer route, you can save some work with Jeff Kelley's answer. For telling if they type it incorrectly, you can have a delegate method compare the correct answer with whatever they are typing.Looking at your overall situation, I'd suggest starting out by understanding text field delegates. Because all of the possible solutions are going to involve that.
William Jockusch
A: 

Check out the delegate methods for UITextFile and UITextView. There are several methods you can implement that are called when the user changes the state of the field. In addition, you can fire off NSTimer if you need something for a particular interval.

Jason McCreary
+2  A: 

Wherever you're handling the change of text, you can start an NSTimer. If more text is entered before the timer fires, you can call -invalidate on it to cancel it, but if the timer fires then you know no text has been entered.

Jeff Kelley
A: 

Register for text changed events on the text field. When a change occurs, create or update a timer member. If the timer fires, your time has elapsed. When cleaning up, invalidate the timer.

-(void) textFieldChanged:(id)sender { // UIControlEventEditingChanged
    NSTimeInterval tooLong = 2.0;

    if ( myTimer ) [myTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:tooLong]];
    else myTimer = [[NSTimer scheduledTimerWithTimeInterval:tooLong ... repeats:NO] retain];
}
drawnonward