UITextField成为FirstFirstReponder无效并返回false

时间:2016-04-06 07:07:35

标签: ios swift uitextfield becomefirstresponder

我的代码中有3个文本字段的出口如下:

    @IBOutlet var oldPasswordTextField: UITextField! { didSet { oldPasswordTextField.delegate = self } }
@IBOutlet var newPasswordTextField: UITextField! { didSet { newPasswordTextField.delegate = self } }
@IBOutlet var confirmPasswordTextField: UITextField! { didSet { confirmPasswordTextField.delegate = self } }

在我的textFieldShouldReturn委托方法中,我想将下一个文本字段作为第一个响应者点击返回,如下所示:

    func textFieldShouldReturn(textField: UITextField) -> Bool {   //delegate method
    textField.resignFirstResponder()

    if textField == oldPasswordTextField
    {
        newPasswordTextField.becomeFirstResponder()
    }
    else if textField == newPasswordTextField
    {
        confirmPasswordTextField.becomeFirstResponder()
    }
    else
    {
        saveButtonTapped()
    }
    return true
}

但它不起作用。所以我试着检查出了什么问题。所以我尝试了这个:

    func textFieldShouldReturn(textField: UITextField) -> Bool {   //delegate method
    if textField.canResignFirstResponder()
    {
        print("textField can resign first responder")
        if textField.resignFirstResponder() == true
        {
            print("textField resigned first responder")
        }
        else
        {
            print("textField didnt resign first responder")
        }
    }
    else
    {
        print("textField cant resign first responder")
    }

    if textField == oldPasswordTextField
    {
        if newPasswordTextField.canBecomeFirstResponder() == true
        {
            if newPasswordTextField.becomeFirstResponder() == true
            {
                print("newPassword field has become first responder")
            }
            else
            {
                print("newPassword field cannot become first responder")
            }
        }

    }
    else if textField == newPasswordTextField
    {
        confirmPasswordTextField.becomeFirstResponder()
    }
    else
    {
        saveButtonTapped()
    }
    return true
}

加时赛我得到了#text;字段不能辞职第一响应者" " newPassword字段不能成为第一响应者" 在我的控制台上。我不知道这里出了什么问题。为什么canResignFirstResponder()方法和becomeFirstResponder()方法返回false?有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

尝试在文本字段中使用标记。这对我有用:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSInteger nextTag = textField.tag + 1;
    // Try to find next responder
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
    if (nextResponder) {
        // Found next responder, so set it.
        [nextResponder becomeFirstResponder];
    } else {
        // Not found, so remove keyboard.
        [textField resignFirstResponder];
    }
    return NO; // We do not want UITextField to insert line-breaks.
}

抱歉,我没有看到它不是Objective-C,很快就会出现:

func textFieldShouldReturn(textField: UITextField!) -> Bool {
    let nextTag:Int = textField.tag + 1
    // Try to find next responder
    if let nextResponder: UIResponder! = textField.superview!.viewWithTag(nextTag){
        nextResponder.becomeFirstResponder()
    } else {
        // Not found, so remove keyboard.
        textField.resignFirstResponder()
    }
    return false; // We do not want UITextField to insert line-breaks.
}

我在这里找到了解决方案:Switching between Text fields on pressing return key in Swift

答案 1 :(得分:0)

知道了。上面的代码很好。我的代码中的错误是我的'textFieldShouldEndEditing'方法返回false,这就是为什么我的resignFirstResponder方法返回false并且它无法正常工作。 所以我让'textFieldShouldEndEditing'方法返回true,它工作正常。 :)

    func textFieldShouldEndEditing(textField: UITextField) -> Bool {  //delegate method
    return true
}