我有一个 UITableViewController,每个单元格中都有一个 UITextField。当用户在一个字段中点击返回时,它会自动将下一个字段设置为第一响应者并滚动表格以显示该字段,如下所示:
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
UITextField *nextTextField=nil;
NSInteger row;
NSInteger section;
if(textField == self.txtFieldA)
{
nextTextField = self.txtFieldB;
row=1; section=0;
}
else if(textField == self.txtFieldB)
{
nextTextField = self.txtFieldC;
row=2; section=0;
}
else
{
[textField resignFirstResponder];
}
if(nextTextField)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[nextTextField becomeFirstResponder];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
当您选择一个文本字段(例如 txtFieldA),向下滚动表格以使 txtFieldB 不可见,然后按 Enter 键时,问题就出现了。它在 iOS5 上按预期工作(txtFieldB 成为新的第一响应者)。但是,在 iOS6 上却没有。它滚动到表格中的正确位置,但 txtFieldB 并没有成为第一响应者(我不确定是什么——当我在导航控制器上点击“返回”时,键盘仍然可见)。
当我点击“返回”时,不会触发 txtFieldB 的 didBeginEditing。如果我延迟[nextTextField becomeFirstResponder]
一秒钟左右(直到滚动动画完成,这使得 nextTextField 可见)它按预期工作,但这样做似乎很笨拙且不太顺利,所以我宁愿了解出了什么问题,也不愿实现类似的东西。