我想在所有uitextfield
上使用第一个响应者辞职。我可以通过将uitextfields
放在一个数组中来完成这个,但我想避免使用该数组。辞职应该发生在所有类型的uiTextField上,如何做到这一点。
这很好用
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var text: UITextField!
@IBOutlet weak var textFieldtwo: UITextField!
var textField = [UITextField]()
override func viewDidLoad() {
super.viewDidLoad()
self.textField = [text,textFieldtwo]
for item in textField {
item.delegate = self
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("text")
for item in textField {
item.resignFirstResponder()
}
}
}
你可以试试这个:
for textField in self.view.subviews where textField is UITextField {
textField.resignFirstResponder()
}
但是,如果您只想通过按return
上的keyboard
或点击屏幕上的任意位置而不使用touchesBegan
来关闭键盘
你可以尝试这个:
// For pressing return on the keyboard to dismiss keyboard
func textFieldShouldReturn(textField: UITextField) -> Bool {
for textField in self.view.subviews where textField is UITextField {
textField.resignFirstResponder()
}
return true
}
...
func hideKeyboard() {
view.endEditing(true)
}
并将此添加到您的viewDidLoad
:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))
view.addGestureRecognizer(tapGesture)
或者只是使用更全球化的方法。 (Swift 3.0)
UIApplication.shared.sendAction(#selector(UIApplication.resignFirstResponder), to: nil, from: nil, for: nil);
当它辞职当前的第一响应者时,这将关闭任何活动字段。祝你好运!
一种更简单的方法是通过以下方式结束对包含UITextFields的UIView的编辑:
view.endEditing(true)
现在在Swift 3中,最简单的方法是
方法1:按下'return'键时调用。
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
textField1.resignFirstResponder()
textField2.resignFirstResponder()
return true;
}
方法2:按下“返回”键时调用。
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
self.view.endEditing(true)
return true;
}
方法3:全局方法在视图中写入加载
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
注意:别忘了添加它。其他明智的不行。
UIGestureRecognizerDelegate, UITextFieldDelegate
我希望它对你有用:)
您可以尝试这样做:1。检查Textfield Delegate
class CoursesViewController: UIViewController,UITextFieldDelegate {
var currentTextField:UITextField!
func addDoneCancel(_ textField : UITextField) {
curentTextField = textField
// ToolBar
let toolBar = UIToolbar()
toolBar.barStyle = .default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
toolBar.sizeToFit()
// Adding Button ToolBar
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(CoursesViewController.doneClick))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(CoursesViewController.cancelClick))
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
textField.inputAccessoryView = toolBar
}
4.工具栏按钮的动作
@objc func doneClick(){
curentTextField.resignFirstResponder()
}
@objc func cancelClick(){
curentTextField.resignFirstResponder()
}
5.UItextField委托
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
curentTextField = textField
addDoneCancel(textField)
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
//delegate method
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
//delegate method
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//delegate method
textField.resignFirstResponder()
return true
}
斯威夫特4:
如果你有IBOutlets:
_ = [textFiledOne, textFiledTwo, textFiledThree].map() { $0.resignFirstResponder() }