I am creating a form with textfields in a tableview controller. I am trying to achieve 2 things here when the return key is pressed after entering value for a field:
1) Move cursor to next field 2) Scroll the tableview up
My code is working for moving the cursor to the next field but the scroll part is not working. Could you let me know what I am missing here. I researched similar issues on stack overflow and am following suggestions from them but still facing issues. Appreciate your inputs.
- (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 ;
}