So i have these textfields that i realised that they all have same properties, so i created new class called \"UserInputs\" and extended from UITextFi
It looks like the UITextFieldDelegate
functions you have in your code are a little off. They should be:
func textFieldDidBeginEditing(textField: UITextField) {
print("focused")
}
func textFieldDidEndEditing(textField: UITextField) {
print("lost focus")
}
And since you want the UserInputs
objects to be their own delegates, I've added that code, too. To demonstrate this, I have the following two files:
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")
}
}
Your UITextFieldDelegate should probably stay in your RegistrationViewController.
Instead of overriding the delegate you can do this. In your init method, add a target to self with the appropriate function.
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
}
//.......//
}
You can change border by calling delegate in 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()
}
}