Arn*_*rne 14 iphone objective-c uitableview first-responder ios
我正在使用这个其他的答案来添加UITextField
s到我的UITableViewCell
s.但是,我现在不知道如何通过使用下一个按钮让用户专注于下一个项目.任何提示?
Anh*_* Do 13
尝试tag
为每个UITextField 分配一个.按下"下一步"按钮后,计算下一个标记并使用[[UIView viewWithTag:nextTag] becomeFirstResponder]
将焦点更改为下一个文本字段.
Alf*_*sen 12
这对我来说效果很好,但必须进行调整以满足您的需求:
#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
HRCFormCell * currentCell = (HRCFormCell *) textField.superview.superview;
NSIndexPath * currentIndexPath = [self.tableview indexPathForCell:currentCell];
if (currentIndexPath.row != [self.questionsArray count] - 1) {
NSIndexPath * nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:0];
HRCFormCell * nextCell = (HRCFormCell *) [self.tableview cellForRowAtIndexPath:nextIndexPath];
[self.tableview scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
[nextCell.textField becomeFirstResponder];
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
Ken*_*and 10
除了Anh发布的关于使用标记值查找下一个字段并将其作为firstResponder的内容...
如果您在UITableView中,还需要记住,下一个UITextField可能不在屏幕上,甚至在视图中,因为它可能位于屏幕底部.在作为第一响应者之前,您可能需要将该新行滚动到视图中.我在之前的应用程序中处理此问题的方法是使标记成为一个值,我可以用它来暗示行的NSIndexPath,这样我就可以找出它所在的行.然后你可以将该行滚动到视图中:
[tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
Run Code Online (Sandbox Code Playgroud)
但是,这会创建一个竞争条件,当您调用becomeFirstResponder时,单元格仍然不可见,所以我会延迟becomeFirstResponder,直到它可见为止.我会设置一个值作为我的类中的属性,如行号,应该成为第一响应者,然后在cellForRowAtIndexPath或willDisplayCell:forDowplayCell:forRowAtIndexPath当我即将显示该单元格时,我将调用isFirstResponder那么文本域......因为它确保存在.
归档时间: |
|
查看次数: |
14329 次 |
最近记录: |