代码无法解雇键盘

时间:2017-07-25 10:45:38

标签: swift xcode keyboard

我正在尝试创建用于在键盘外部触摸或按下返回按钮时解除键盘的代码。但这是不可行的:D这就是我在这里问的原因。我怎样才能使它工作?它没有给出错误,但它没有响应。

如果有更简单的解决方案,那么也欢迎提供。

感谢您的时间和回答!

这是我的完整代码:

import UIKit

class EntryViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var UserEmailAdresInput: UITextField!

    @IBOutlet weak var UserPasswordInput: UITextField!

    @IBOutlet weak var UserPasswordInputRepeated: UITextField!

    @IBOutlet weak var UserSignUp: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.UserEmailAdresInput.delegate = self
        self.UserPasswordInput.delegate = self
        self.UserPasswordInputRepeated.delegate = self


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func SignUp(_ sender: Any) {

        let UserEmail = UserEmailAdresInput.text;
        let UserPassword = UserPasswordInput.text;
        let UserPasswordRepeated = UserPasswordInputRepeated.text;

        //Hide Keyboard upon Touch

        func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
            self.view.endEditing(true)
        }

        //Hide Keyboard upon Return Key
        func textFieldShouldReturn(textField: UITextField) -> Bool {
            UserEmailAdresInput.resignFirstResponder()
            return (true)


        }

        //Check if fields are filled in correctly

        if(UserEmail?.isEmpty == true || UserPassword?.isEmpty == true || UserPasswordRepeated?.isEmpty == true){

            displayAlertMessage(userMessage: "Alle velden moeten ingevuld worden");
            return;

        }

        if(UserPassword != UserPasswordRepeated){

            displayAlertMessage(userMessage: "Wachtwoorden zijn niet gelijk");
            return;
        }

        //Store Data
        UserDefaults.standard.set(UserEmail, forKey: "UserEmail");
        UserDefaults.standard.set(UserPassword, forKey: "UserPassword");
        UserDefaults.standard.synchronize();

        //SignUp Succesfull
        var Alert = UIAlertController(title:"Succesvol aangemeld!", message: "Ga naar je email inbox om je aanmelding te voltooien", preferredStyle: UIAlertControllerStyle.alert);

        let OkAction = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default, handler: nil);

        Alert.addAction(OkAction);
        self.present(Alert, animated: true, completion: nil)


    }

    func displayAlertMessage(userMessage:String){

        var Alert = UIAlertController(title:"Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.alert);

        let OkAction = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default, handler: nil);

        Alert.addAction(OkAction);

        self.present(Alert, animated: true, completion: nil)

    }

}

1 个答案:

答案 0 :(得分:1)

UITextField下面的Delegate方法将用于在键盘上的返回按钮上按下文本字段。

func textFieldShouldReturn(textField: UITextField) -> Bool {
            if textField == UserEmailAdresInput{
                 UserEmailAdresInput.resignFirstResponder()
            } else if textField == UserPasswordInput{
                 UserPasswordInput.resignFirstResponder()
            } else {
                 UserPasswordInputRepeated.resignFirstResponder()
            }
            return true
   }

并将你的touchesBegan和textFieldShouldReturn方法放在signUp方法之外。