看起来
UITextFieldDelegate您的代码中的功能有些偏离。他们应该是:
func textFieldDidBeginEditing(textField: UITextField) { print("focused")} func textFieldDidEndEditing(textField: UITextField) { print("lost focus")}
并且由于您希望
UserInputs对象成为它们自己的委托,因此我也添加了该代码。为了证明这一点,我有以下两个文件:
ViewController.swift
import UIKitclass 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 UIKitclass UserInputs: UITextField, UITextFieldDelegate { required init(prer aDeprer: NSCoder) { super.init(prer: aDeprer)! 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") }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)