Domanda

Ho una vista tabella raggruppata che contiene 3 sezioni e ciascuna riga per sezione. Le prime due righe sezione contiene UITextField (Nome e soggetto sono i titoli delle sezioni) e l'ultimo contiene UITextView (Messaggio è il titolo della sezione) perché voglio ottenere alcuni dati da parte dell'utente da questo controller stesso.

I due campi di testo hanno la returnKeyType come UIReturnKeyNext. Per UITextView, il pulsante "ritorno" è presente in tastiera per alimentare nuova linea. Così ho usato metodo textFieldShouldReturn per spostarsi alla cella successiva premendo questi pulsanti tipo ritorno UIKeyboard.

Il pulsante successivo funzionerà benissimo con il primo campo di testo (Nome). Qui il problema arriva ... Se si fa clic sul pulsante Avanti della seconda cella, si va al UITextView (ultima cella) con una riga in basso. Cioè, il cursore si sposta di una riga Oltre alla sua posizione originale.

Il mio codice è ...

-(BOOL) textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    if (textField == nameTextField) {
        [subjectTextField becomeFirstResponder];    
    }
    else if(textField == subjectTextField) {
        [messageTextView becomeFirstResponder];
    }
    return YES;
}

Che cosa devo fare per rendere questo bel lavoro? Grazie in anticipo ..

È stato utile?

Soluzione

While testing a lot of stuff I found a simple yet suitable solution:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ( [textField isEqual: nameTextField] )
    {
        [nameTextField resignFirstResponder];
        [messageTextView becomeFirstResponder];
        return NO;
    }

    return YES;
}

It handles the resigning of the nameTextField itself and returns NO to the request.

Altri suggerimenti

Basically what is happening is that when return is tapped you make text view the first responder and the return gets added to the text view. Thats why the cursor goes to the second line. Try doing this in your textViewDidChange: delegate method:

- (void)textViewDidChange:(UITextView *)textView {
        if(textView.text == @"\r") {
        textView.text = @"";
}

In this link you will get solution of your problem. In this given that how we hide keyboard when text editing done.

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