【问题标题】:Swift how to resign first responder on all uiTextfieldSwift 如何让所有 uiTextfield 上的第一响应者辞职
【发布时间】:2016-05-14 16:26:11
【问题描述】:

我想在所有 uitextfield 上使用 resign 第一响应者。我可以通过将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


    【解决方案1】:

    或者只是使用更全球化的方法。 (斯威夫特 3.0)

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

    这将关闭任何活动字段,因为它会辞去当前的第一响应者。 祝你编码好运!

    【讨论】:

    • 这可以安全使用吗,我的意思是,苹果会批准使用此代码提交到应用商店的应用吗?
    【解决方案2】:

    你可以试试这个:

    for textField in self.view.subviews where textField is UITextField {
        textField.resignFirstResponder()
    }
    

    但是,如果您只想关闭键盘,方法是按keyboard 上的return 或在不使用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)
    

    【讨论】:

    【解决方案3】:

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

    view.endEditing(true)
    

    【讨论】:

    • "view.endEditing(true)" 适用于开始触摸,但不适用于 "textFieldShouldReturn"
    【解决方案4】:

    现在在 Swift 3 中,最简单的方法是

    方法一: 按下“返回”键时调用。

    func textFieldShouldReturn(_ textField: UITextField) -> Bool 
     {
     textField1.resignFirstResponder()
     textField2.resignFirstResponder()
            return true;
        }
    

    方法2:在按下'return'键时调用。

    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
    

    我希望它对你有用:)

    【讨论】:

    • 当我使用它时,它会阻止任何其他触摸事件触发.. 非常适合关闭键盘,但通过视图呈现不可编辑
    • @nbpeth 是的,我可以理解您的问题,这是因为在方法 3 UIView 是所有其他视图的父级。
    【解决方案5】:

    resignFirstResponder 带有工具栏按钮 Swift4 ##

    你可以试试这个: 1. 检查 Textfield Delegate
    class CoursesViewController: UIViewController,UITextFieldDelegate {

    1. 创建一个 TextField 变量: 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
    }
    

    【讨论】:

      【解决方案6】:

      斯威夫特 4:

      如果您有 IBOutlets:

      _ = [textFiledOne, textFiledTwo, textFiledThree].map() { $0.resignFirstResponder() }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多