1

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?

user717452
  • 33
  • 14
  • 73
  • 149
  • 1
    Text views are meant for entering multiple lines of text. The return key is meant for entering those multiple lines of text. You need another way to indicate that the focus should move to the next field. – rmaddy May 08 '14 at 19:23

3 Answers3

0

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.

Community
  • 1
  • 1
Sergei Nikitin
  • 788
  • 7
  • 12
  • 1
    There is no such thing as the `textViewShouldReturn` method. – rmaddy May 08 '14 at 19:21
  • Sorry, my answer is incomplete. You are right - this function must be defined first. My fault. – Sergei Nikitin May 08 '14 at 19:23
  • Defined how? How will it be called? – rmaddy May 08 '14 at 19:24
  • Thru `textViewDidChange` or `textView: shouldChangeTextInRange:`. First way in my answer is just a variant of second way, sorry. – Sergei Nikitin May 08 '14 at 19:26
  • You really should clarify your answer. As it stands, it sound like there are two choices but the 1st one isn't valid on its own. – rmaddy May 08 '14 at 19:27
  • As one of possible variants it can be defined as `- (BOOL)textViewShouldReturn:(UITextView *)textView { [self performSelector:@selector(textViewDidChange:) withObject:textView]; return NO; }` – Sergei Nikitin May 08 '14 at 19:31
  • But there is no such delegate method as `textViewShouldReturn:`. It won't ever get called. – rmaddy May 08 '14 at 19:33
  • I found this solution in `UIInputToolbar` by Brandon Hamilton (https://github.com/brandonhamilton/inputtoolbar/tree/master/UIInputToolbarSample/Classes/UIInputToolbar). There you can find necessary code for definition of this function and delegate. This function calls `- (BOOL)expandingTextViewShouldReturn:(BHExpandingTextView *)expandingTextView;` – Sergei Nikitin May 08 '14 at 20:00
0

One thing that I like to do when I have multiple textfields, is use the tag instead of checking one by one. First, in the interface builder/storyboard, set the tag property of each UITextField in ascending order, (it's better if you start at 1). Than, your textFieldShouldReturn: reduces to something like this.

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
    UIView * nextView = [self.scrollView viewWithTag:textField.tag + 1];
    if ([nextView respondsToSelector:@selector(becomeFirstResponder)])
        [nextView becomeFirstResponder];
    return NO;
}

And about your question about going to the next field from a UITextView, I agree with the second option from Sergey's answer

Gonzo
  • 1,533
  • 16
  • 28
  • Why would you see if the view responds to `becomeFirstResponder`? `UIView` extends `UIResponder` so all views respond to that selector. – rmaddy Mar 09 '17 at 21:50
0

I know this is an old post but since I managed to get my answer, I thought I share it.

- (BOOL) textFieldShouldReturn:(UITextField *)textField 
{
    [textView performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.1];

    return YES;
}

I was told that with the afterDelay:0.1 it makes a difference in the device, where using simulator we can use afterDelay:0.0.

Rach
  • 1
  • 5