触摸的文本字段不起作用

时间:2017-01-24 12:06:03

标签: ios swift swift3 uitextfield skyfloatinglabeltextfield

我正在尝试在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'

例外的原因是什么?

2 个答案:

答案 0 :(得分: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")
}

答案 1 :(得分: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
    }
}