代码不会解雇键盘

时间:2016-01-01 07:07:03

标签: ios swift keyboard dismiss

我添加了resignFirstResponder和touchesBegan到我的代码,但键盘没有消失。我已经检查了代表并将代表分配给了文本域,但仍然没有解雇的迹象。

请帮助发现错误? (我猜它可能是关于多个textFields的?)

import UIKit
import Parse

class SignUpViewController: UIViewController, UITextFieldDelegate {

    @IBAction func BackToFirstPageButton(sender: AnyObject) {

        performSegueWithIdentifier("BackToLogInPage", sender: self)
    }

    func SignOutForEmailVerification() {

        PFUser.logOut()
        performSegueWithIdentifier("BackToLogInPage", sender: self)
    }

    @IBOutlet var UsernameTextField: UITextField!

    @IBOutlet var PasswordTextField: UITextField!

    @IBOutlet var EmailTextField: UITextField!

    var ActivityIndicator:UIActivityIndicatorView = UIActivityIndicatorView()

    @IBAction func SignUpButton(sender: AnyObject) {

        if UsernameTextField.text == "" || PasswordTextField.text == "" || EmailTextField.text == "" {

            let SignUpAlert = UIAlertController (title: "Error in form", message: "Please fill in the blanks", preferredStyle: UIAlertControllerStyle.Alert)

            SignUpAlert.addAction((UIAlertAction(title: "Dismiss", style: .Default, handler: { (action) -> Void in

            })))

            self.presentViewController(SignUpAlert, animated: true, completion: nil)

        } else {

            ActivityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
            ActivityIndicator.center = self.view.center
            ActivityIndicator.hidesWhenStopped = true
            ActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
            view.addSubview(ActivityIndicator)
            ActivityIndicator.startAnimating()
            UIApplication.sharedApplication().beginIgnoringInteractionEvents()

            var user = PFUser()
            user.username = UsernameTextField.text
            user.password = PasswordTextField.text
            user.email = EmailTextField.text

            user.signUpInBackgroundWithBlock({ (success, error) -> Void in

                self.ActivityIndicator.stopAnimating()
                UIApplication.sharedApplication().endIgnoringInteractionEvents()

                if error == nil {

                    let EmailVerificationAlert = UIAlertController(title: "Email Verification", message: "Please click the link in an email we have just sent you", preferredStyle: UIAlertControllerStyle.Alert)

                    EmailVerificationAlert.addAction(UIAlertAction(title: "Okay", style: .Default, handler: { EmailVerificationAlert in self.SignOutForEmailVerification()})
                )

                    self.presentViewController(EmailVerificationAlert, animated: true, completion: nil)
            }

        })
        }

    func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.UsernameTextField.delegate = self
        self.PasswordTextField.delegate = self
        self.EmailTextField.delegate = self

    }

    func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.
    }

        func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

            self.view.endEditing(true)

        }

        func textFieldShouldReturn(textField: UITextField!) -> Bool {

            textField.resignFirstResponder()

            return true

        }    
    }    
}

2 个答案:

答案 0 :(得分:1)

当用户点击键盘上的“返回”键时

由于您已添加 <script type="text/javascript"> $(function () { $('#datetimepicker').datetimepicker({ minDate: new Date() }); }); </script> ,请尝试修改此代码:

UITextFieldDelegate

当用户点击键盘上的按钮返回时,这将有效。

您的错误就像您所说的处理多个文本字段一样。你还必须说出你想要哪一个resignFirstResponder()。

当用户点击屏幕时

用户还可以通过点击屏幕上的任意位置来关闭键盘。 在这里,我们有一个func textFieldShouldReturn(textField: UITextField!) -> Bool { self.UsernameTextField.resignFirstResponder(); self.EmailTextField.resignFirstResponder(); self.PasswordTextField.resignFirstResponder(); return true; } 来检测点击以解除键盘。这是一种替代解决方案。

UITapGestureRecognizer

答案 1 :(得分:0)

v2.8..v3.0