代码不解除键盘
codes are not dismissing keyboard
我在我的代码中添加了 resignFirstResponder 和 touchesBegan,但键盘没有消失。我已经检查了委托并将委托分配给文本字段,但仍然没有解雇的迹象。
请帮忙找出错误? (我猜它可能与多个文本字段有关?)
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
}
}
}
import UIKit
import Parse
class SignUpViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var UsernameTextField: UITextField!
@IBOutlet var PasswordTextField: UITextField!
@IBOutlet var EmailTextField: UITextField!
var ActivityIndicator:UIActivityIndicatorView = UIActivityIndicatorView()
var currentTextField : UITextField?
func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.UsernameTextField.delegate = self
self.PasswordTextField.delegate = self
self.EmailTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func BackToFirstPageButton(sender: AnyObject) {
performSegueWithIdentifier("BackToLogInPage", sender: self)
}
func SignOutForEmailVerification() {
PFUser.logOut()
performSegueWithIdentifier("BackToLogInPage", sender: self)
}
@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 {
currentTextField?.resignFirstResponder();
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 textFieldDidBeginEditing(textField: UITextField!){
currentTextField = textField;
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
currentTextField.resignFirstResponder()
return true
}
}
}
当用户点击键盘上的 "Return" 键时
因为您已经添加了 UITextFieldDelegate
,请尝试修改此代码:
func textFieldShouldReturn(textField: UITextField!) -> Bool {
self.UsernameTextField.resignFirstResponder();
self.EmailTextField.resignFirstResponder();
self.PasswordTextField.resignFirstResponder();
return true;
}
这将在用户点击键盘上的按钮 return 时起作用。
您的错误正如您所说的处理多个文本字段。另外你必须说出你想要 resignFirstResponder() 的哪一个。
当用户点击屏幕时
用户还可以通过点击屏幕上的任意位置来关闭键盘。
这里我们有一个 UITapGestureRecognizer
检测关闭键盘的点击。这是一个替代解决方案。
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
我在我的代码中添加了 resignFirstResponder 和 touchesBegan,但键盘没有消失。我已经检查了委托并将委托分配给文本字段,但仍然没有解雇的迹象。
请帮忙找出错误? (我猜它可能与多个文本字段有关?)
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
}
}
}
import UIKit
import Parse
class SignUpViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var UsernameTextField: UITextField!
@IBOutlet var PasswordTextField: UITextField!
@IBOutlet var EmailTextField: UITextField!
var ActivityIndicator:UIActivityIndicatorView = UIActivityIndicatorView()
var currentTextField : UITextField?
func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.UsernameTextField.delegate = self
self.PasswordTextField.delegate = self
self.EmailTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func BackToFirstPageButton(sender: AnyObject) {
performSegueWithIdentifier("BackToLogInPage", sender: self)
}
func SignOutForEmailVerification() {
PFUser.logOut()
performSegueWithIdentifier("BackToLogInPage", sender: self)
}
@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 {
currentTextField?.resignFirstResponder();
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 textFieldDidBeginEditing(textField: UITextField!){
currentTextField = textField;
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
currentTextField.resignFirstResponder()
return true
}
}
}
当用户点击键盘上的 "Return" 键时
因为您已经添加了 UITextFieldDelegate
,请尝试修改此代码:
func textFieldShouldReturn(textField: UITextField!) -> Bool {
self.UsernameTextField.resignFirstResponder();
self.EmailTextField.resignFirstResponder();
self.PasswordTextField.resignFirstResponder();
return true;
}
这将在用户点击键盘上的按钮 return 时起作用。
您的错误正如您所说的处理多个文本字段。另外你必须说出你想要 resignFirstResponder() 的哪一个。
当用户点击屏幕时
用户还可以通过点击屏幕上的任意位置来关闭键盘。
这里我们有一个 UITapGestureRecognizer
检测关闭键盘的点击。这是一个替代解决方案。
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}