在自定义 UlTableViewCell 中使用 UITextField 时 resignFirstResponder 崩溃

Posted

技术标签:

【中文标题】在自定义 UlTableViewCell 中使用 UITextField 时 resignFirstResponder 崩溃【英文标题】:resignFirstResponder crashes while using UITextField in custom UlTableViewCell 【发布时间】:2013-01-14 23:41:33 【问题描述】:

我的应用程序出现间歇性崩溃。崩溃日志让我认为 UITextField 的 resignFirstResponser 被调用的方式似乎存在一些问题。我在我的自定义 tableview 单元格中使用 UITextField 和 UITextFieldDelegate 协议方法,这会导致间歇性崩溃。我在下面发布崩溃日志。请任何可以帮助我了解崩溃根本原因的建议或 cmets 都会很棒。此外,似乎崩溃主要发生在 ios 6 上

谢谢。

以下是 Crittercism 崩溃日志:

0 libobjc.A.dylib 0x39d585b0 objc_msgSend + 16 + 15 1 UIKit 0x3477f165-[UITextField_resignFirstResponder] + 457 + 456 2 UIKit 0x34664249-[UIResponder resignFirstResponder] + 281 + 280 3 UIKit 0x34712397-[UITextField resignFirstResponder] + 147 + 146 4 UIKit 0x346962f5-[UITableView reloadData] + 225 + 224 5 BF 0x0008ed37 -[BFOpenBetsController updateFilterData] (BFOpenBetsController.m:768) 6 BF 0x0008d695 -[BFOpenBetsController deleteBet:] (BFOpenBetsController.m:566) 7 BF 0x00094fbd -[BFOpenBetsUnMatchedCell userTapOnButton:] (BFOpenBetsUnMatchedCell.m:198) 8 UIKit 0x347380a5-[UIApplication sendAction:to:from:forEvent:] + 73 + 72 9 UIKit 0x34738057-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 31 + 30 10 UIKit 0x34738035-[UIControl sendAction:to:forEvent:] + 45 + 44 11 UIKit 0x347378eb-[UIControl(内部)_sendActionsForEvents:withEvent:] + 503 + 502 12 UIKit 0x34737de1-[UIControl touchesEnded:withEvent:] + 489 + 488 13 UIKit 0x34656421 _UIGestureRecognizerUpdate + 5769 + 5768 14 核心基础 0x351536cd __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 21 + 20 15 核心基础 0x351519c1 __CFRunLoopDoObservers + 277 + 276 16 核心基础 0x35151d17 __CFRunLoopRun + 743 + 742 17 核心基础 0x350c4ebd CFRunLoopRunSpecific + 357 + 356 18 核心基础 0x350c4d49 CFRunLoopRunInMode + 105 + 104 19 图形服务 0x32a172eb GSEventRunModal + 75 + 74 20 UIKit 0x346a12f9 UIApplicationMain + 1121 + 1120

【问题讨论】:

崩溃的场景是什么?只是显示后还是滚动后等会崩溃吗? @AkshayShah:实际上,这是一个 Crittercism 日志。用户在 Live 版本中看到它,我无法重现它,因此它似乎是一个间歇性问题。我的理解是它会在稍后重新加载 tableView 时发生,所以可能就像滚动或更新 tableView 肯定不是在初始数据显示后的乞讨中。同样,即使滚动也不会一直崩溃。所以我想可能是我的 textField 被选中,然后 tableView 被重新加载或其他东西,但我不确定只是想从日志中找出问题。 【参考方案1】:

您的一个 UITextField 可能正在被释放(就像它们在滚动出视图时那样,或者在重新加载视图时)。看到你有一个 TextField 委托,它可能不知道该字段不再存在,所以当它调用 resign 第一响应者时,它会崩溃。

解决方案是在自定义单元格的 dealloc 例程中调用 uitextfield setDelegate:nil。

见https://***.com/a/9484805/899996

【讨论】:

【参考方案2】:

似乎在更新过滤器数据时,您重新加载了表格视图,而此时一些 UITextField 处于编辑模式。 尝试用这样的东西重现。

如果这是问题所在,请先在所有活动的UITextField 上尝试resignFirstResponder,然后再在UITableView 上调用reloadData

【讨论】:

以上是关于在自定义 UlTableViewCell 中使用 UITextField 时 resignFirstResponder 崩溃的主要内容,如果未能解决你的问题,请参考以下文章

在自定义单元 iphone 中使用按钮传输变量

为啥在自定义身份验证和登录模块中使用克隆?

iOS - 如何在自定义单元格中使用继承?

在自定义类中使用setMouseCallBack

在自定义 segue 中使用 destinationViewController 按钮

如何在自定义 CollectionView 中使用 UIScrollViewDelegate?