我无法隐藏键盘

时间:2016-06-16 08:38:26

标签: ios swift keyboard

帮助我,PLZ! 将一个键盘更改为另一个TextField时,我无法隐藏键盘。

我点击"用户" textfield pop-up Picker,点击"密码" textfield,一个弹出式键盘,但如果再次点击" User"不是隐藏键盘。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        userNameTextField.resignFirstResponder()
        userPasswordTextField.resignFirstResponder()

}

func textFieldDidBeginEditing(textField: UITextField) {

    if textField == userNameTextField  {
        pickerUser.hidden = false
                   print("userNameTextField")

    } else {
        pickerUser.hidden = true
        print("@userPasswordTextField")
    }


}

@IBAction func userNameTextFieldStartEdit(sender: AnyObject) {

    userNameTextField.resignFirstResponder()
    userPasswordTextField.resignFirstResponder()
    pickerUser.hidden = false
}


func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

enter image description here

3 个答案:

答案 0 :(得分:2)

下面只添加一行:

func textFieldDidBeginEditing(textField: UITextField) {

   if textField == userNameTextField  {
       textField.resignFirstResponder()      // this line add
       pickerUser.hidden = false
               print("userNameTextField")

   } else {
       pickerUser.hidden = true
        print("@userPasswordTextField")
   }


}

答案 1 :(得分:2)

使用此代码,

func textFieldDidBeginEditing(textField: UITextField) {

        if textField == userNameTextField  {
            pickerUser.hidden = false
            print("userNameTextField")
            self.view.endEditing(true)

        } else {
            pickerUser.hidden = true
            print("@userPasswordTextField")
        }

    }


    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        if(textField == userNameTextField) {
            self.view.endEditing(true)
            return true
        }
        return true
    }

它为我工作,希望它有用

答案 2 :(得分:0)

我认为应该调用隐藏键盘的功能:

func textFieldShouldReturn(textField: UITextField) -> Bool 
{
    textField.resignFirstResponder()
    return true
}

而不是pickerUser.hidden = true 还有:

class ViewController: UIViewController, UITextFieldDelegate {

(你有那个吗?)