我创建了一个带有一些箭头键的键盘,允许用户在表格视图中的所有文本字段之间使用箭头按钮进行导航。(每个单元格 2 个)
在某些情况下单元格将被禁用,所以我有一个递归调用来跳过它们。IE。如果用户按下右箭头,并且 textField 被禁用,则再次递归调用右箭头以跳过它。
我还为需要自己完成的任何滚动设置动画。但是,最近发生了一些事情,当用户导航到新的 textField 并完成滚动动画后尝试设置 firstResponder 时,会导致 EXEC_BAD_ACCESS。
我能够从第一个单元格开始重现此问题并向下导航 2 次。这是代码,按照我可以显示的最好的顺序排列,并进行了很多编辑(就像其他 3 个箭头按钮的所有代码一样,所以你只看到调用的行)
//receive the arrow button pressed first
_arrowButton = arrowButton;
@try
{
switch (_arrowButton.tag)
{
case NumericKeyboardViewDownArrow:
destinationIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:currentIndexPath.section];
newTag = currentTextField.tag;
break;
default:
break;
}
// check bounds of the intended index path and return if out of bounds
if ((destinationIndexPath.row == -1) || (destinationIndexPath.row >= [_tableView numberOfRowsInSection:[destinationIndexPath section]])) {
destinationIndexPath = currentIndexPath;
return;
}
/*
Animation for scrolling
*/
CGPoint current = [_tableView contentOffset];
// if we must animate up
if (destinationIndexPath.row < currentIndexPath.row)
{
// up code here
}
// if we must animate down
else if (destinationIndexPath.row > currentIndexPath.row)
{
// always scroll animate for down
[_tableView setContentOffset:CGPointMake(0, current.y + _tableView.rowHeight) animated:YES];
}
// if we only move left or right
else
{
// left and right code here
}
//update our current location
currentIndexPath = destinationIndexPath;
@catch (NSException *e)
{
return;
}
然后在动画完成动画方法中完成动画后
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
//after the animation is done, set the responder
nextTextFieldSelection = (UITextField *)[[_tableView cellForRowAtIndexPath:currentIndexPath] viewWithTag:newTag];
NSLog(@"nexttextfield%@",nextTextFieldSelection);
if (nextTextFieldSelection)
{
//CRASH OCCURS HERE [nextTextFieldSelection becomeFirstResponder];
}
[self checkTextFieldIsDisabled];
}
在该方法结束时,我在一个新的文本字段中,我们检查是否禁用。在重现此崩溃时,它只是退出,因为我要进入的 textFields 没有被禁用。
在调用新的 becomeFirstResponder 之前,我尝试让第一响应者辞职,但这似乎没有任何效果。不过,我可能没有在合适的位置辞职。有什么想法会出现内存问题吗?
谢谢