我正在使用UITextfied,而点击textfied键盘出现,但当我按下返回键,键盘没有消失。我使用了以下代码:
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
return true;
}
方法resignfirstresponder没有进入函数。
我正在使用UITextfied,而点击textfied键盘出现,但当我按下返回键,键盘没有消失。我使用了以下代码:
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
return true;
}
方法resignfirstresponder没有进入函数。
当前回答
你应该连接UITextfied和一个视图控制器的委托来调用这个函数
其他回答
在你正在使用的视图控制器中:
//suppose you are using the textfield label as this
@IBOutlet weak var emailLabel: UITextField!
@IBOutlet weak var passwordLabel: UITextField!
//then your viewdidload should have the code like this
override func viewDidLoad() {
super.viewDidLoad()
self.emailLabel.delegate = self
self.passwordLabel.delegate = self
}
//then you should implement the func named textFieldShouldReturn
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
// -- then, further if you want to close the keyboard when pressed somewhere else on the screen you can implement the following method too:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true);
}
当用户点击文本键盘上的Done按钮时,将生成Did End on Exit事件;那时,我们需要告诉文本字段放弃控制,这样键盘就会消失。为了做到这一点,我们需要向控制器类添加一个动作方法。 选择ViewController.swift添加以下动作方法:
@IBAction func textFieldDoneEditing(sender: UITextField) {
sender.resignFirstResponder()}
选择主要。项目导航器中的故事板,并调出连接检查器。从Did End On Exit旁边的圆圈拖动到故事板中的黄色视图控制器图标,然后松开。将出现一个小弹出菜单,其中包含我们刚刚添加的单个操作的名称。单击textFieldDoneEditing动作来选择它,就是这样。
我讨厌给每个UIViewController添加相同的函数。 通过扩展UIViewController来支持UITextFieldDelegate,你可以提供一个默认行为“返回按下”。
extension UIViewController: UITextFieldDelegate{
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
}
当你创建新的UIViewController和UITextField时,你所要做的就是在你的UIViewController中写一行代码。
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
你甚至可以通过在Main.storyboard中钩子委托来省略这一行代码。(使用“ctrl”从UITextField拖动到UIViewController)
所有在一个隐藏键盘和移动键盘打开视图:Swift 5
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(taped))
view.addGestureRecognizer(tap)
NotificationCenter.default.addObserver(self, selector: #selector(KeyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(KeyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func taped(){
self.view.endEditing(true)
}
@objc func KeyboardWillShow(sender: NSNotification){
let keyboardSize : CGSize = ((sender.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size)!
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
}
}
@objc func KeyboardWillHide(sender : NSNotification){
let keyboardSize : CGSize = ((sender.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size)!
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
另一种主要使用故事板并轻松允许你拥有多个文本字段的方法是:
@IBAction func resignKeyboard(sender: AnyObject) {
sender.resignFirstResponder()
}
将视图控制器的所有文本字段连接到每个字段的Did End on Exit事件上的那个动作。