2016-01-14 58 views
6

Così ho questi campi di testo che mi sono reso conto che tutti hanno stesse proprietà, così ho creato nuova classe denominata "UserInputs" ed esteso da UITextField, tutto funziona correttamente tranne una cosa, UITextFieldDelegate funzioni doesn' t lavoro, voglio dire quando mi concentro su di loro non funziona, voglio aggiungerlo in codice perché quando ti concentri sui miei campi di input cambiano confine, come faccio correttamente sottoclasse da UITextFieldcorrettamente Subclassing UITextField in rapida

gli unici problemi che ho quelle funzioni:

textFieldDidBeginEditing 
textFieldDidEndEditing 

e quindi non funziona.

questo è il mio classe dove tutto accade:

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 
} 

} 

Questo è il mio sottoclasse:

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

questo codice funzionato quando non avevo sottoclasse e stavo facendo dentro la mia principale classe, ma dopo aver creato le funzioni di focus della sottoclasse ha smesso di funzionare, tutto il resto funziona

.935.846.510,40321 milioni problema principale è che voglio implementare

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

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

questi nei miei campi di testo in modo che io non scrivo più e più volte

risposta

8

Sembra che il UITextFieldDelegate funzioni che avete nel vostro codice sono un po 'fuori . Essi dovrebbero essere:

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

E poiché si desidera che gli oggetti UserInputs di essere i propri delegati, ho aggiunto che il codice, anche. Per dimostrare questo, ho i seguenti due file:

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

forse, ma non ha risolto il problema, quando scrivo il codice non v'è alcun suggerimento su: textFieldDid ... ma quando scrivo nella classe principale che mi dà suggerimenti – nikagar4

+1

Questi metodi delegato appartengono nel delegato e non all'interno della classe UserInputs. –

+0

non funziona ancora, posterò la classe principale in modo da poter vedere completamente quello che sto facendo – nikagar4

0

È possibile modificare confine dal delegato chiamando 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() 
    } 
} 
5

Probabilmente il tuo UITextFieldDelegate rimane nel tuo RegistrationViewController.

Invece di eseguire l'override del delegato, è possibile farlo. Nel metodo init, aggiungi un target a self con la funzione appropriata.

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 
    } 
//.......// 
} 
+0

Questo è quello che stavo cercando. In questo modo non si blocca alcuna vista dall'implementazione di MVC in futuro, grazie! :) –

+0

Questa dovrebbe essere la risposta accettata. Fa ciò che l'OP vuole e non interferisce con l'uso dei delegati. – Grimxn