Swift - UITextFields 的默认行为

Swift - Default behavior for UITextFields

我有一个 UITextField 的扩展,它想在活动/不活动/无效时更改样式。我想在自定义中进行所有这些操作,以避免重复代码。

当我在自定义文本字段外(在视图中)设置它时效果很好,但当我想这样设置时会抛出异常。并且在字段变为活动状态(聚焦)时抛出异常,而不是在它出现时抛出。

class LoginTextField: UITextField {
@IBInspectable var strokeColor: UIColor = UIColor.captionColor()

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.addTarget(self, action: "setFocusOn:", forControlEvents: UIControlEvents.EditingDidBegin)
}

func setFocusOn(){
    strokeColor = UIColor.captionUnderlineColor()
}

例外情况:

BlockquoteTerminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CustomTextField setFocusOn:]: unrecognized selector sent to instance 0x7ff048d0b270'

任何 help/hint 将不胜感激!

TIA, 弥尔顿

您的选择器没有参数,因此不需要冒号,所以请尝试:

    self.addTarget(self, action: "setFocusOn", forControlEvents: UIControlEvents.EditingDidBegin)