以编程方式从inputAccessoryView执行textFieldShouldReturn

时间:2016-02-01 14:52:00

标签: ios swift uitextfield

以编程方式执行的方式(模拟) func textFieldShouldReturn(textField:UITextField) - > Bool 从输入附件视图中运行。

MyCustomInputView有DONE按钮。我想要 func textFieldShouldReturn(textField:UITextField) - >按下DONE按钮时,将为MyTextField代理触发Bool 。实现它的方法是什么?

class MyTextField: UITextField {

    func setup() {
       let myCustomInputView = MyCustomInputView()
       self.inputAccessoryView = myCustomInputView
    }
}

1 个答案:

答案 0 :(得分:0)

我做了MyCustomInputView将具有UITextField的扩展功能。

class MyCustomInputView: UIView {
    var textField: UITextField?

    @IBAction func didClickReturnButton(sender: UIButton) {
        if let textField = textField, let textFieldDelegate = textField?.delegate {
            if textFieldDelegate.textFieldShouldReturn!(textField) {
                textField.endEditing(true)
            }
        }
    }
}