Swift - Default behavior for UITextFields

45 views Asked by At

I've an extension of UITextField that want to change the style when is active / inactive / invalid. I wanted to make all this in a customization in order to avoid code being repeated.

It works great when I set this outside the custom textfield (in the view) but throws an exception when I want to set like this. And the exception is thrown when the field becomes active (focused), not when it appears.

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()
}

Here is the exception:

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

Any help/hint will be much appreciated !

TIA, Milton

1

There are 1 answers

1
Ali Beadle On BEST ANSWER

Your selector has no arguments so does not need the colon, so try:

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