如何观察用户在UIAlertController中编辑textField的情况?

时间:2019-10-01 10:11:15

标签: swift xcode uitextfield uialertcontroller

我有一个UIAlertController和一个textField。由于我无法为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

让我知道您是否也希望看到我的代码。

2 个答案:

答案 0 :(得分:2)

您可以尝试使用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)
    }

}

答案 1 :(得分:0)

您不需要保留对文本字段的引用。您可以像这样在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")
    }
}