【问题标题】:How do I observe user editing a textField in an UIAlertController?如何观察用户在 UIAlertController 中编辑文本字段?
【发布时间】:2019-10-01 10:11:15
【问题描述】:

我有一个UIAlertController 和一个textField。由于我无法为textField 创建插座,我必须找到另一种方法来获取实时观察用户输入的功能。

我做了一些尝试,但没有成功。该线程解释了如何将目标添加到文本字段:

How do I check when a UITextField changes?

textfield.addTarget(self, action: #selector(ViewControllerr.textFieldDidChange(_:)), for: UIControl.Event.editingChanged)

我试过这个,但不太明白#selector 输入。它想要我的UIViewController? 还是我的UIAlertController 我都试过了,但得到了同样的错误信息:

Value of type 'UIViewController' has no member 'textFieldDidChange'

UIAlertController 相同。

我还尝试将我的textField 设置为委托访问该类的功能:

textField.delegate = self as? UITextFieldDelegate

但我想这不是正确的方法,因为我仍然无法访问任何功能,除非这样做:

textField.delegate?.textFieldDidBeginEditing?(textField)

但这也没有给我任何东西。

我也尝试添加这样的扩展:

extension UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        print("????EDITING")
}// became first responder

    func textFieldDidEndEditing(_ textField: UITextField) {
        print("????EDITING")
    }
}

但由于某种原因,我无法通过textView 访问它们

如果我像这样扩展类:

extension PickNumberViewController: UITextFieldDelegate {...}

我收到同样的错误信息

Value of type 'UITextField' has no member 'textFieldDel...'

这没有任何意义,因为我将 textField 设置为 UITextFieldDelegate 而不是 UITextField

如果您也想查看我的代码,请告诉我。

【问题讨论】:

    标签: swift xcode uitextfield uialertcontroller


    【解决方案1】:

    您可以尝试使用self.textF = alert.textFields?[0] 获取对它的引用

    class ViewController: UIViewController {
    
        var textF:UITextField!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
    
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
    
                //1. Create the alert controller.
                let alert = UIAlertController(title: "Some Title", message: "Enter a text", preferredStyle: .alert)
                alert.addTextField { (textField) in
                    textField.text = "Some default text"
                }
                 let tex = alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
    
                })) 
                // 4. Present the alert.
                self.present(alert, animated: true, completion: nil)
    
                self.textF =  alert.textFields?[0]
    
                self.textF.addTarget(self, action: #selector(self.textFieldDidChange), for: UIControl.Event.editingChanged)
    
            }
    
    
        }
    
        @objc func textFieldDidChange() {
            print(textF.text)
        }
    
    }
    

    【讨论】:

    • 哇,成功了!你真棒!非常感谢哥们!不过,我不完全理解你最后在那里做了什么。你愿意详细说明一下吗?再次感谢您抽出宝贵时间:)
    【解决方案2】:

    您不需要保留对文本字段的引用。您可以像这样在 addTextField 闭包中添加目标或设置委托。

    class ViewController: UIViewController {
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
            alert.addTextField { textField in
                textField.delegate = self
                textField.addTarget(self, action: #selector(self.textChanged(_:)), for: .editingChanged)
                textField.placeholder = "Enter name"
            }
            alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
            present(alert, animated: true, completion: nil)
        }
        @objc func textChanged(_ sender: UITextField) {
            print("Text changed - ", sender.text!)
        }
    }
    extension ViewController: UITextFieldDelegate {
        func textFieldDidBeginEditing(_ textField: UITextField) {
            print("EDITING")
        }
        func textFieldDidEndEditing(_ textField: UITextField) {
            print("EDITING")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      相关资源
      最近更新 更多