Move From UITextField to UITextView When Return Pressed

╄→гoц情女王★ 提交于 2019-12-11 12:44:57

问题


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?


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/23550496/move-from-uitextfield-to-uitextview-when-return-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!