Question

I have the following workflow:

Input in Textfield -> (Return key of TextField pressed: resignFirstResponder, IBAction of Button called) -> IBAction of Button: perform an intensive operation

The problem is that the intensive operation is started before the Keyboard is dismissed with resignFirstResponder. As in other posts suggested (e.g. https://stackoverflow.com/a/3452767/1685971) I tried to use:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    [self performSelector:@selector(ibActionMethod:) withObject:sender afterDelay:someTime];
    return YES;
}

This works fine in simulator or on iPhone 4 but leads to a crash running on iPhone 5 (what I cannot understand, because I didn't use performSelectorInBackground:withObject:):

void _WebThreadLockFromAnyThread(bool): Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread.
bool _WebTryThreadLock(bool): Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

Another option I tried is to wait for resignFirstResponder in main thread with

[textField performSelectorOnMainThread:@selector(resignFirstResponder) withObject:nil waitUntilDone:YES];

But with this the keyboard is still not dismissed immediately (probably because the method waits only for the return of the selector resignFirstResponder and not for the finishing of the keyboard animation.

Is there any solution to this?

Was it helpful?

Solution

Try this

dispatch_async(dispatch_get_main_queue(), ^{
    // your button action
});

i was also facing the same error,get solved by above trick.

OTHER TIPS

try this code,

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

       [textField resignFirstResponder];

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //your code here

        });

       return YES;

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top