Pregunta

En mi solicitud, hay una vista de tabla con 2 campos de texto por celda.Estoy asignando etiquetas para cada campo de texto.Al presionar el botón Hecho, quiero realizar el siguiente campo de texto el primer respondedor (puede estar entre las celdas de la tabla).Pero el código ahora estoy usando no funciona.Adjuntando el presente código

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

        }

    }

}

No estoy obteniendo los campos de texto en la matriz de Sub View.Estoy usando una celda personalizada para celdas de mesa.Por favor ayuda.

¿Fue útil?

Solución

Con poca modificación al código de Karim, puede acceder a los textvies como este:

- (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];
}

Espero que esto ayude.

Otros consejos

Defina un método como se enumera a continuación -

- (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;
}

en otra parte,

UITextField *nextField = [self nextToRespondAfterTextField:textField];
[nextField becomeFirstResponder];

Dado que está utilizando la etiqueta para identificar su vista de texto.Otra forma puede ser usar los delegados de visualización de texto.

- (void)textViewDidEndEditing:(UITextView *)textView {
int currentTag = textView.tag;
int nextTag = currentTag + 1; // or something else
UITextView *nextTV = [self.tableView viewWithTag: nextTag];
[nextTV becomesFirstResponder];
}

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top