2

我有以下工作流程:

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

问题是密集操作是在键盘被解除之前开始的resignFirstResponder。正如在其他帖子中建议的那样(例如https://stackoverflow.com/a/3452767/1685971),我尝试使用:

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

这在模拟器或 iPhone 4 上运行良好,但会导致在 iPhone 5 上运行崩溃(我无法理解,因为我没有使用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...

我尝试的另一个选择是在主线程中等待 resignFirstResponder

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

但是这样键盘仍然没有立即关闭(可能是因为该方法只等待选择器的返回resignFirstResponder而不是键盘动画的完成。

有什么解决办法吗?

4

2 回答 2

2

试试这个

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

我也面临同样的错误,通过上述技巧解决。

于 2013-02-19T09:36:55.993 回答
0

试试这个代码,

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

       [textField resignFirstResponder];

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

        });

       return YES;

}

于 2013-02-19T08:51:16.567 回答