1

我正在尝试在 UITextField 上设置 ontouch 侦听器,但它根本不起作用这是我的代码:

class SettingsVC: BaseVC {


    @IBOutlet weak var languageField: SkyFloatingLabelTextField!
    @IBOutlet weak var btnburgerMenu: UIButton!
    @IBOutlet weak var headerLbl: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        languageField.addTarget(self, action: "myTargetFunction:", for: UIControlEvents.touchDown)
    }

    func myTargetFunction(textField: SkyFloatingLabelTextField) {
        print("test")
    }

}

这是我的错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[IFO.SettingsVC myTargetFunction:]: unrecognized selector sent to instance 0x7fac8b61ff30'

异常的原因是什么?

4

2 回答 2

1

应该是这样——

languageField.addTarget(self, action: #selector(YourViewController.myTargetFunction(sender:)), for: UIControlEvents.touchDown)

或像这样使用

languageField.addTarget(self, action: #selector(self.myTargetFunction(_:)), forControlEvents: .touchDown)

并调用函数

func myTargetFunction(_ textField: SkyFloatingLabelTextField) {
    print("test")
}
于 2017-01-24T12:08:57.783 回答
0

.touchDown 实际上没什么用,因为它并不总是触发。每次触摸 UITextField 时,我都需要一种让对话框出现的方法。我改用这个:

class DateTextField: DefaultTextField, UITextFieldDelegate {
    ...
    override init() {
        super.init()
        self.delegate = self // delegate to handle textFieldShouldBeginEditing below
        inputView = UIView() // disable the input view
    }

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        // Code to display my date picker dialog
        return false // return false to disable textfield editing
    }
}
于 2020-03-17T21:13:59.897 回答