快速正确地继承UITextField

快速正确地继承UITextField,第1张

快速正确地继承UITextField

看起来

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5171599.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存