gpt4 book ai didi

ios - 代码不解除键盘

转载 作者:行者123 更新时间:2023-11-29 01:21:22 25 4
gpt4 key购买 nike

我在我的代码中添加了 resignFirstResponder 和 touchesBegan,但键盘没有消失。我已经检查了委托(delegate)并将委托(delegate)分配给文本字段,但仍然没有解雇的迹象。

请帮忙找出错误? (我猜它可能与多个文本字段有关?)

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

}
}
}

最佳答案

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

因为您已经添加了 UITextFieldDelegate,请尝试修改此代码:

func textFieldShouldReturn(textField: UITextField!) -> Bool {
self.UsernameTextField.resignFirstResponder();
self.EmailTextField.resignFirstResponder();
self.PasswordTextField.resignFirstResponder();

return true;
}

这将在用户点击键盘上的返回按钮时起作用。

您的错误在于您所说的处理多个文本字段。另外你必须说出你想要 resignFirstResponder() 的哪一个。

当用户点击屏幕时

用户还可以通过点击屏幕上的任意位置来关闭键盘。这里我们有一个 UITapGestureRecognizer 可以检测关闭键盘的点击。这是一个替代解决方案。

override func viewDidLoad() {
super.viewDidLoad()

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}

func dismissKeyboard() {

view.endEditing(true)
}

关于ios - 代码不解除键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34554272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com