Domanda

Ho un UITableViewController raggruppato con una sezione e molte righe. Ogni cell è costituito da due elementi: un UILabel con una descrizione e un UITextField per un ingresso. Una forma, direi.

In fondo è un pulsante "Avanti", vai alla vista successiva. Ma prima di ciò, convalido i generacodicitagcode: se l'utente non ha riempito un campo, questo campo dovrebbe ottenere la messa a fuoco, in modo che l'utente veda che ha bisogno di entrare in qualcosa. Ho provato con questo:

[inputField becomeFirstResponder];
.

Ricorda, il pulsante che ho premuto è sul fondo della mia vista e immagina che il UITextField sia sulla parte superiore . In questa situazione, il campo non fa attenzione perché è troppo lontano. Ora quando scorlo lentamente e il campo diventa visibile, il campo diventa il primo responder e ottiene il cursore :-) Conclusione: il UITextField ha funzionato, ma non fa esattamente ciò che volevo. Non voglio scorrere con il mio dito, il campo dovrebbe prendere la messa a fuoco e visibile automaticamente.

C'è un modo per "saltare" direttamente nel mio campo? Utilizzare un'alternativa a becomeFirstResponder? Scorri fino al mio campo automaticamente?

Grazie per il tuo aiuto!

È stato utile?

Soluzione

If you know the index of cell with the UITextField you want to edit, use



NSIndexPath* indexPath = [[NSIndexPath indexPathWithIndex:your_section_index] indexPathByAddingIndex:your_cell_index];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]

before you call becomeFirstResponder.

If the cell is off the screen, becomeFirstResponder may fail for some versions of iOS. In addition, some versions will perform additional scrolling on the tableview in some situations (e.g. if Apple's code determines that the responder would still be off the screen without more scrolling). To get around these issues, you may need to call become first responder after the scroll animation is complete.

Altri suggerimenti

I'd the same issue. it is due to the UIAnimation occurring for the two tasks simultaneously. So while the UIAnimation for scrollToRowAtIndexPath ignores the becomeFirstResponder .

The solution might be disable the Animation effect one of it OR else using the performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:1.0f];.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top