1

在我的应用程序中,有一个表格视图,每个单元格有 2 个文本字段。我为每个文本字段分配标签。在按下完成按钮时,我想让下一个文本字段成为第一响应者(可以在表格单元格之间)。但是我现在使用的代码不起作用。附上当前代码

    NSArray *viewsToBecomeFirstResponder = [tableView subviews];
for (UIView *firstResponder in viewsToBecomeFirstResponder) {
    if ([firstResponder isKindOfClass:[UITextField class]]) {
        if (firstResponder.tag == nextTag ) {
            [firstResponder becomeFirstResponder];

        }

    }

}

我没有得到子视图数组中的文本字段。我正在为表格单元格使用自定义单元格。请帮忙。

4

3 回答 3

1

只需对 Karim 的代码稍作修改,您就可以像这样访问 textVies:

- (void)textViewDidEndEditing:(UITextView *)textView {
   //Get the indexPath for the currently selected cell
   NSIndexPath *cellPath = [(UITableView*)[self view] indexPathForSelectedRow];

   //Get the actual cell
   CustomTableCell *cell = (CustomTableCell *)[(UITableView*)[self view] cellForRowAtIndexPath:cellPath];

   int currentTag = textView.tag;
   int nextTag = currentTag + 1; // or something else

   UITextView *nextTV = [cell viewWithTag: nextTag];
   [nextTV becomesFirstResponder];
}

希望这可以帮助。

于 2011-05-21T20:50:32.143 回答
1

定义如下所列的方法——</p>

- (UITextField *) nextToRespondAfterTextField:(UITextField*)textField {
    NSInteger nextTag = textField.tag + 1;

    CustomTableCell *theCell = (CustomTableCell*)[textField.superview];
    for ( UIView *aView in theCell.subviews ) {
        if ( [aView isKindOfClass:[UITextField class]]) {
            if ( aView.tag == nextTag ) {
                return aView;
            }
        }
    }

    NSIndexPath *indexPath = [self.tableView indexPathForCell:theCell];

    NSInteger section          = indexPath.section;
    NSInteger row              = indexPath.row;
    NSInteger numberOfSections = [self.tableView numberOfSections];

    if ( [self.tableView numberOfRowsInSection:section] != (row + 1) ) {
        indexPath = [NSIndexPath indexPathForRow:(row + 1) inSection:section];
    } else {
        do {
            section++;
        } while ( section < numberOfSections && [self.tableView numberOfRowsInSection:section] == 0 ) ;

        if ( section == numberOfSections ) {
            return nil;
        } else {
            indexPath = [NSIndexPath indexPathForRow:0 inSection:section];
        }
    }

    theCell = (CustomTableCell*)[self.tableView cellForRowAtIndexPath:indexPath];
    if ( theCell == nil ) {
        return nil;
    }

    for ( UIView *aView in theCell.subviews ) {
        if ( [aView isKindOfClass:[UITextField class]]) {
            if ( aView.tag == nextTag ) {
                return aView;
            }
        }
    }   

    return nil;
}

别处,

UITextField *nextField = [self nextToRespondAfterTextField:textField];
[nextField becomeFirstResponder];
于 2011-05-21T22:07:57.240 回答
0

由于您使用标签来识别您的文本视图。另一种方法是使用文本视图代理。

- (void)textViewDidEndEditing:(UITextView *)textView {
int currentTag = textView.tag;
int nextTag = currentTag + 1; // or something else
UITextView *nextTV = [self.tableView viewWithTag: nextTag];
[nextTV becomesFirstResponder];
}
于 2011-05-21T11:28:17.553 回答