我正在 tableview 控制器中创建一个带有文本字段的表单。在为字段输入值后按下返回键时,我试图在这里实现两件事:
1) 将光标移动到下一个字段 2) 向上滚动表格视图
我的代码正在将光标移动到下一个字段,但滚动部分不起作用。你能告诉我我在这里缺少什么吗?我研究了有关堆栈溢出的类似问题,并遵循他们的建议,但仍然面临问题。感谢您的投入。
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if ([textField isEqual:self.firstName]){
//Move cursor to next field
[self.lastName becomeFirstResponder];
//scroll
id cellContainingFirstResponder = textField.superview.superview ;
NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
[self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
if ([textField isEqual:self.lastName]){
//Move cursor to next field
[self.emailId becomeFirstResponder];
//scroll
id cellContainingFirstResponder = textField.superview.superview ;
NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
[self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
if ([textField isEqual:self.emailId]){
//Move cursor to next field
[self.phoneNumber becomeFirstResponder];
//scroll
id cellContainingFirstResponder = textField.superview.superview ;
NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
[self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
if ([textField isEqual:self.phoneNumber]){
//Move cursor to next field
[self.password becomeFirstResponder];
//scroll
id cellContainingFirstResponder = textField.superview.superview ;
NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
[self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
// This is the last field hence dismiss keyboard -> This part is working
if ([textField isEqual:self.password]){
[textField resignFirstResponder];
}
return YES ;
}