Properly Subclassing UITextField in swift

Also habe ich diese Textfelder, dass ich festgestellt habe, dass sie alle die gleichen Eigenschaften haben, also habe ich eine neue Klasse namens " UserInputs "und erweitert vonUITextField, alles funktioniert einwandfrei mit Ausnahme einer Sache,UITextFieldDelegate -Funktionen funktionieren nicht, ich meine, wenn ich mich auf sie konzentriere, funktioniert es nicht, ich möchte es in Code einfügen, denn wenn Sie sich auf meine Eingabefelder konzentrieren, ändern sie den RahmeUITextField

ie einzigen Probleme, die ich habe, sind die Funktionen:

textFieldDidBeginEditing
textFieldDidEndEditing

und so funktioniert es nicht.

das ist meinKlass wo alles passiert:

import Foundation

import UIKit

class RegistrationViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var firstName: UserInputs!
@IBOutlet weak var test: UserInputs!

override func viewDidLoad() {
    super.viewDidLoad()
    self.firstName.delegate = self
    self.test.delegate = self
}

}

Das ist meinUnterklass:

class UserInputs: UITextField{

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    createBorder()
}
func createBorder(){
    let border = CALayer()
    let width = CGFloat(2.0)
    border.borderColor = UIColor(red: 55/255, green: 78/255, blue: 95/255, alpha: 1.0).CGColor
    border.frame = CGRect(x: 0, y: self.frame.size.height-width, width: self.frame.size.width, height: self.frame.size.height)
    border.borderWidth = width
    self.layer.addSublayer(border)
    self.layer.masksToBounds = true
    //print("border created")
}
func textFieldDidBeginEditing() {
    print("focused")
    self.pulseBorderColor()
}
func textFieldDidEndEditing() {
    print("lost focus")
    self.reversePulseBorderColor()
}
func pulseBorderColor(){
    let pulseAnimation = CABasicAnimation(keyPath: "borderColor")
    pulseAnimation.duration = 0.35
    pulseAnimation.fromValue = UIColor(red: 55/255, green: 78/255, blue: 95/255, alpha: 1.0).CGColor
    pulseAnimation.toValue = UIColor(red: 252/255, green: 180/255, blue: 29/255, alpha: 1.0).CGColor
    pulseAnimation.fillMode = kCAFillModeForwards
    pulseAnimation.removedOnCompletion = false
    pulseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    self.layer.sublayers![0].addAnimation(pulseAnimation,forKey: nil)
    }
func reversePulseBorderColor(){
    let pulseAnimation = CABasicAnimation(keyPath: "borderColor")
    pulseAnimation.duration = 0.35
    pulseAnimation.fromValue = UIColor(red: 252/255, green: 180/255, blue: 29/255, alpha: 1.0).CGColor
    pulseAnimation.toValue = UIColor(red: 55/255, green: 78/255, blue: 95/255, alpha: 1.0).CGColor
    pulseAnimation.fillMode = kCAFillModeForwards
    pulseAnimation.removedOnCompletion = false
    pulseAnimation.timingFunction = CAMediaTimingFunction(name:  kCAMediaTimingFunctionEaseInEaseOut)
    self.layer.sublayers![0].addAnimation(pulseAnimation,forKey: nil)
    }
}

dieser Code funktionierte, wenn ich keine Unterklasse hatte und es in meiner Hauptklasse tat, aber nachdem ich Fokusfunktionen für Unterklassen erstellt hatte, funktionierte alles andere

Hauptproblem ist, dass ich @ implementieren möch

func textFieldDidBeginEditing() {
    print("focused")
}

func textFieldDidEndEditing() {
    print("lost focus")
}

dies in meinen Textfeldern, damit ich es nicht immer wieder schreibe

Antworten auf die Frage(6)

Ihre Antwort auf die Frage