iphone - resignFirstResponder 出现后续处理问题(键盘在 iPhone 5 上被阻止或崩溃)

标签 iphone ios thread-safety uitextfield resignfirstresponder

我有以下工作流程:

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的返回,而不是等待键盘动画的完成。

有什么解决办法吗?

最佳答案

试试这个

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

我也遇到了同样的错误,通过上面的技巧解决了。

关于iphone - resignFirstResponder 出现后续处理问题(键盘在 iPhone 5 上被阻止或崩溃),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14953134/

相关文章:

ios - 核心数据按月和总和分组

ios - 自定义删除不响应 ToSelector

ios - 我可以将 in.xyz 作为包 ID 吗?

java - ConcurrentLinkedQueue poll() 产生损坏的数据?

Python队列链接对象运行异步协程与主线程输入

c# - 将不同的值添加到并发字典中的列表中

iphone - 无法使用 NSDateFormatter 解析日期

ios - #define 字符串格式中的值?

iphone - UITextView : Text starts from 2nd line of text view

iphone - 如何为开发配置我的 iOS 设备?