iOS app “next” key won't go to the next text field

前端 未结 11 2502
栀梦
栀梦 2020-12-12 21:32

I have a simple scene (using storyboard in IB) with a Username and Password text box. I\'ve set the keyboard to close when you are on the Password text field but can\'t get

相关标签:
11条回答
  • 2020-12-12 21:39

    You have to add the UITextFieldDelegate in the header-file. Then you have to set

    theTextField.delegate = self;
    

    in the viewDidLoad-method. After that you can go on with

    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
        if (theTextField == self.textPassword) {
            [theTextField resignFirstResponder];
        } else if (theTextField == self.textUsername) {
            [self.textPassword becomeFirstResponder];
        }
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-12 21:40

    You can use this subclass of UITextfield that dynamically change the UIReturnKey according to text/string condition, and then perform your continuation action in the textFieldShouldReturn:(UITextField *)textField delegate

    if (textField.returnKeyType == UIReturnKeyNext)
    
    https://github.com/codeinteractiveapps/OBReturnKeyTextField
    
    0 讨论(0)
  • 2020-12-12 21:47

    The return key doesn't do anything special in a text field by default. You need to explicitly change the first responder:

    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
        if (theTextField == self.textPassword) {
            [theTextField resignFirstResponder];
        } else if (theTextField == self.textUsername) {
            [self.textPassword becomeFirstResponder];
        }
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-12 21:47

    I use this code that allows me to control the form behavior in the storyboard:

    -(BOOL) textFieldShouldReturn:(UITextField *)textField{
        if(textField.returnKeyType==UIReturnKeyNext) {
            UIView *next = [[textField superview] viewWithTag:textField.tag+1];
            [next becomeFirstResponder];
        } else if (textField.returnKeyType==UIReturnKeyDone) {
            [textField resignFirstResponder];
        }
        return YES;
    } 
    

    All you need is to assign the order value as tag, and returnkey according to Next or Done on each input control.

    0 讨论(0)
  • 2020-12-12 21:48

    I got an issue that password textfield get focus and then lose focus immediately. At last I found that we'd better return NO in function textFieldShouldReturn:. If we return YES, the text field will get a insertText: call. Then it will lose focus. Don't know the reason.

    I have created a new question for this issue: UITextField get focus and then lose focus immediately due to return YES in textFieldShouldReturn

    If anyone knows the reason, please let me know. Thanks.

    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
        if (theTextField == self.textPassword) {
            [theTextField resignFirstResponder];
        } else if (theTextField == self.textUsername) {
            [self.textPassword becomeFirstResponder];
        }
        // we'd better return NO here
        return NO;
    }
    
    0 讨论(0)
  • 2020-12-12 21:48

    Here is a Swift 2.0 Version of Rob Mayoff

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        if textField == self.emailTextField {
            self.passwordTextField.becomeFirstResponder()
        }else{
            self.passwordTextField.resignFirstResponder()
        }
        return false
    }
    
    0 讨论(0)
提交回复
热议问题