Search code examples
iosuitextfielduitextviewresignfirstresponderbecomefirstresponder

Move From UITextField to UITextView When Return Pressed


I have several text views in my app. All but one are UITextField, and the other is a UITextView, located right in the middle of the others. This is due to this section needing much more room for the user to type and see all of what they are typing out. I have it set up so that when the user presses next on the keyboard, it goes to the next field, using this code:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == firstName) {
        [firstName resignFirstResponder];
        [lastName becomeFirstResponder];
    } else if (textField == lastName) {
        [lastName resignFirstResponder];
        [theRequest becomeFirstResponder];
    }
    else if (textField == theRequest) {
        [theRequest resignFirstResponder];
        [theDetails becomeFirstResponder];
    }
    else if (textField == theDetails) {
        [theDetails resignFirstResponder];
        [textdate becomeFirstResponder];
    }
    else if (textField == textdate) {
        [textdate resignFirstResponder];
        [location becomeFirstResponder];
    }
    else if (textField == location) {
        [location resignFirstResponder];

    }
    return YES;
}

Everything goes fine until I get to the UITextView. When I click next there, it simply pushes the cursor down a line. How can I get it to respond to simply go to the next field?

Also, when clicking Next in location, it goes back to first name. How can that be setup to just dismiss keyboard?


Solution

  • You can try two ways:

    1) Use function

    -(BOOL)textViewShouldReturn:(UITextView *)textView
    

    to realize needed behaviour.

    2) Read this:

    How to dismiss keyboard for UITextView with return key?

    and adapt those solutions to your neeeds.