我想在所有uitextfields
上使用第一个响应者。我可以通过将 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()
}
}
}
放在数组中来完成此操作但我想避免使用该数组。辞职应该发生在所有类型的uiTextField上,如何做到这一点。
这很好用
.where()
答案 0 :(得分:25)
或者只是使用更全球化的方法。 (Swift 3.0)
UIApplication.shared.sendAction(#selector(UIApplication.resignFirstResponder), to: nil, from: nil, for: nil);
这将解除任何活动字段,因为它会撤消当前的第一响应者。 祝你好运!
答案 1 :(得分:20)
你可以试试这个:
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)
答案 2 :(得分:13)
更简单的方法是在包含UITextFields的UIView上结束编辑,说:
view.endEditing(true)
答案 3 :(得分:3)
现在在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
我希望它对你有用:)
答案 4 :(得分:0)
你可以试试这个:
1. 检查文本字段代表
var currentTextField:UITextField!
创建一个TextField var: 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
}
创建“完成”&amp;在TextField
中“取消”按钮工具栏@objc func doneClick(){
curentTextField.resignFirstResponder()
}
@objc func cancelClick(){
curentTextField.resignFirstResponder()
}
4. 工具栏按钮的操作
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
}
5. UItextField委托
$s1="32.56.86.90.23";
$s2="11.25.32.90.10";
答案 5 :(得分:-1)
快捷键4:
如果您有IBOutlets:
_ = [textFiledOne, textFiledTwo, textFiledThree].map() { $0.resignFirstResponder() }