在swift中正确地对UITextField进行子类化

时间:2016-01-14 06:04:57

标签: ios swift uitextfield subclass uitextfielddelegate

所以我有这些文本字段,我意识到它们都具有相同的属性,所以我创建了一个名为" UserInputs "并且从UITextField扩展,一切正常,除了一件事,UITextFieldDelegate函数不起作用,我的意思是当我专注于它们它不起作用时,我想将它添加到代码,因为当你专注于我的输入字段时,它们会改变边界,我如何从UITextField

正确地进行子类化

我遇到的唯一问题是功能:

textFieldDidBeginEditing
textFieldDidEndEditing

所以没有工作。

这是我的,一切都在这里发生:

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
}

}

这是我的子类

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

当我没有子类并且在我的主类中执行此代码时,此代码有效,但在创建子类后,焦点函数停止工作,其他一切正常工作

主要问题是我想实现

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

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

这些在我的文本区域中,所以我不能一遍又一遍地写它

3 个答案:

答案 0 :(得分:14)

您的UITextFieldDelegate可能应该保留在您的RegistrationViewController中。

您可以执行此操作,而不是覆盖委托。 在init方法中,使用适当的函数将目标添加到self。

class UserInputs: UITextField {
  override init(frame: CGRect) {
      super.init(frame: frame)
      self.addTarget(self, action: "formatText", for: .editingChanged)
  }

  required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
      self.addTarget(self, action: "formatText", for: .editingChanged)
  }

  func formatText() {
     // Edit self.text here
  }
//.......//
}

答案 1 :(得分:13)

您的代码中的UITextFieldDelegate函数看起来有些偏差。他们应该是:

func textFieldDidBeginEditing(textField: UITextField) {
    print("focused")
}
 func textFieldDidEndEditing(textField: UITextField) {
    print("lost focus")
}

由于您希望UserInputs个对象成为他们自己的代理,我也添加了该代码。为了证明这一点,我有以下两个文件:

<强> ViewController.swift

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    var textField: UserInputs!

    override func viewDidLoad() {
        super.viewDidLoad()
        textField = UserInputs(frame: CGRectMake(100, 100, 200, 40))
        view.addSubview(textField!)
    }
}

<强> UserInputs.swift

import UIKit

class UserInputs: UITextField, UITextFieldDelegate {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        delegate = self
        createBorder()
    }
    required override init(frame: CGRect) {
        super.init(frame: frame)
        delegate = self
        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(textField: UITextField) {
        print("focused")
    }
    func textFieldDidEndEditing(textField: UITextField) {
        print("lost focus")
    }
}

答案 2 :(得分:0)

您可以通过在UserInput

中调用委托来更改边框
class UserInputs: UITextField, UITextFieldDelegate{

    let border = CALayer()

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        createBorder()
        self.delegate = self

    }

    func createBorder(){
        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 pulseBorderColor(){
        border.borderColor = UIColor.greenColor().CGColor
    }

    func normalColor(){
        border.borderColor = UIColor(red: 55/255, green: 78/255, blue: 95/255, alpha: 1.0).CGColor
    }

    func textFieldDidBeginEditing(textField: UITextField) {
        print("focused")
        pulseBorderColor()
    }

    func textFieldDidEndEditing(textField: UITextField) {
        print("lost focus")
        normalColor()
    }
}