按下 return 键时如何在下一个 UITextField 上移动控件
How to shift the control on the next UITextField when return key is press
我有一个 UITableViewController
自定义 UITableViewCell
。其中每个单元格包含一个 UITextField
。现在我想要的是在按下 return 键时将控件从一个文本字段转移到每个下一个文本字段。
我要做的是这个...
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSIndexPath *currentIndexPath=[self.tableView indexPathForCell:(UITableViewCell*)textField.superview.superview];
if(currentIndexPath.row<_excerciseData.totalBlanks-1){
NSIndexPath nextIndexPath=???
UITextField *nextTextFeild=[(UITableViewCell*)[self.tableView cellForRowAtIndexPath:nextIndexPath] viewWithTag:2];
[nextTextFeild becomeFirstResponder];
}else{
[textField resignFirstResponder];
}
return YES;
}
但是我不知道如何找到下一个 UITableViewCell
的 indexPath。谁能帮我找到这个?这...:)
因为您已经有一个自定义 table 视图单元格,最简单的解决方案是添加一个 属性 来保存索引路径。然后在您 return 单元格的委托方法中,只需将当前索引路径分配给稍后可能使用的单元格。
这对于节中的最后一个单元格或 table 视图本身来说效果不是很好。你应该做的是从单元格本身向 table 视图所有者(视图控制器)报告事件,然后它可以计算下一个单元格索引是什么:如果你有多个部分,它必须检查它是否应该去一个新的部分,如果它是最后一个单元格,则什么都不做(或者如果你愿意,可以转到 table 视图的开头)。
对于转发消息,您可以尝试此处的答案中提到的程序之一:
明确一点,文本字段委托应该是单元格本身!
最好你可以这样做,
CGPoint pointInTable = [field convertPoint:field.bounds.origin toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
参考:How to get a UITableViewCell from one of its subviews
或者这样做,
id view = [textfield superview];
while (view && [view isKindOfClass:[UITableViewCell class]] == NO) {
view = [view superview];
}
UITableViewCell *tableViewCell = (UITableViewCell *)view;
参考:How to get UITableView from UITableViewCell?
更好的方法是使用文本字段的 tag 属性。这样做吧。
当您使用 cellForRowAtIndexPath
创建单元格时
为单元格内的 textField 提供标记值,例如:
cell.yourTextField.tag = indexPath.row + someConstant; // someConstant is just to make sure that there is no textfield with the same tag value
并在您的 textField 委托中:- (BOOL)textFieldShouldReturn:(UITextField *)textField
执行以下操作。
// Jumping with the keyboard Next button implementation.
if (UIReturnKeyNext == textField.returnKeyType) //may be u can avoid this check
{
id nextField = [self viewWithTag:textField.tag + 1];
if ([nextField isKindOfClass:[UITextField class]])
{
[nextField becomeFirstResponder];
}
}
希望 textFieldDelegates 在 tableview 的所有者中实现 class。
解决方法在这里
#prgma mark - UITableViewDelegate Methods
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *stringCell = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.yourTextField.tag = indexPath.row;
return cell;
}
#prgma mark - UITextFieldDelegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSUInteger index = textField.tag;
UITextField *nextTextField = (UITextField*)[self.view viewWithTag:index+1];
[nextTextField becomeFirstResponder];
return YES;
}
我有一个 UITableViewController
自定义 UITableViewCell
。其中每个单元格包含一个 UITextField
。现在我想要的是在按下 return 键时将控件从一个文本字段转移到每个下一个文本字段。
我要做的是这个...
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSIndexPath *currentIndexPath=[self.tableView indexPathForCell:(UITableViewCell*)textField.superview.superview];
if(currentIndexPath.row<_excerciseData.totalBlanks-1){
NSIndexPath nextIndexPath=???
UITextField *nextTextFeild=[(UITableViewCell*)[self.tableView cellForRowAtIndexPath:nextIndexPath] viewWithTag:2];
[nextTextFeild becomeFirstResponder];
}else{
[textField resignFirstResponder];
}
return YES;
}
但是我不知道如何找到下一个 UITableViewCell
的 indexPath。谁能帮我找到这个?这...:)
因为您已经有一个自定义 table 视图单元格,最简单的解决方案是添加一个 属性 来保存索引路径。然后在您 return 单元格的委托方法中,只需将当前索引路径分配给稍后可能使用的单元格。
这对于节中的最后一个单元格或 table 视图本身来说效果不是很好。你应该做的是从单元格本身向 table 视图所有者(视图控制器)报告事件,然后它可以计算下一个单元格索引是什么:如果你有多个部分,它必须检查它是否应该去一个新的部分,如果它是最后一个单元格,则什么都不做(或者如果你愿意,可以转到 table 视图的开头)。
对于转发消息,您可以尝试此处的答案中提到的程序之一:
明确一点,文本字段委托应该是单元格本身!
最好你可以这样做,
CGPoint pointInTable = [field convertPoint:field.bounds.origin toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
参考:How to get a UITableViewCell from one of its subviews
或者这样做,
id view = [textfield superview];
while (view && [view isKindOfClass:[UITableViewCell class]] == NO) {
view = [view superview];
}
UITableViewCell *tableViewCell = (UITableViewCell *)view;
参考:How to get UITableView from UITableViewCell?
更好的方法是使用文本字段的 tag 属性。这样做吧。
当您使用 cellForRowAtIndexPath
创建单元格时
为单元格内的 textField 提供标记值,例如:
cell.yourTextField.tag = indexPath.row + someConstant; // someConstant is just to make sure that there is no textfield with the same tag value
并在您的 textField 委托中:- (BOOL)textFieldShouldReturn:(UITextField *)textField
执行以下操作。
// Jumping with the keyboard Next button implementation.
if (UIReturnKeyNext == textField.returnKeyType) //may be u can avoid this check
{
id nextField = [self viewWithTag:textField.tag + 1];
if ([nextField isKindOfClass:[UITextField class]])
{
[nextField becomeFirstResponder];
}
}
希望 textFieldDelegates 在 tableview 的所有者中实现 class。
解决方法在这里
#prgma mark - UITableViewDelegate Methods
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *stringCell = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.yourTextField.tag = indexPath.row;
return cell;
}
#prgma mark - UITextFieldDelegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSUInteger index = textField.tag;
UITextField *nextTextField = (UITextField*)[self.view viewWithTag:index+1];
[nextTextField becomeFirstResponder];
return YES;
}