Swift如何在所有uiTextfield上辞职第一响应者

问题描述 投票:20回答:6

我想在所有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()
            } 
        }
    }
swift uitextfielddelegate
6个回答
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)

25
投票

或者只是使用更全球化的方法。 (Swift 3.0)

UIApplication.shared.sendAction(#selector(UIApplication.resignFirstResponder), to: nil, from: nil, for: nil);

当它辞职当前的第一响应者时,这将关闭任何活动字段。祝你好运!


15
投票

一种更简单的方法是通过以下方式结束对包含UITextFields的UIView的编辑:

view.endEditing(true)

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

我希望它对你有用:)


0
投票

带工具栏按钮的resignFirstResponder Swift4 ##

您可以尝试这样做:1。检查Textfield Delegate class CoursesViewController: UIViewController,UITextFieldDelegate {

  1. 创建一个TextField var:var currentTextField:UITextField!
  2. 在TextField中创建“完成”和“取消”按钮工具栏 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
}

-1
投票

斯威夫特4:

如果你有IBOutlets:

_ = [textFiledOne, textFiledTwo, textFiledThree].map() { $0.resignFirstResponder() }
© www.soinside.com 2019 - 2024. All rights reserved.