2016-01-14 77 views
6

所以我有這些文本框,我才意識到它們都具有相同的屬性,所以我創建了一個名爲「UserInputs」,並從UITextField擴展新的類,一切工作正常,除了一兩件事,UITextFieldDelegate功能沒有按」 t的工作,我的意思是當我專注於他們這是行不通的,我想在代碼中添加它,因爲當你把注意力集中在我的輸入字段上時,他們會改變邊界,我如何恰當地從子類別UITextField正確子類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") 
} 

這些在我的文本框,所以我不過來寫,一遍又一遍

回答

8

它看起來像你必須在你的代碼是有點過了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") 
    } 
} 
+0

也許,但它並沒有解決問題,當我寫的代碼有上沒有建議:textFieldDid ......但是當我在主類寫它給了我建議 – nikagar4

+1

這些委託方法屬於在委託而不是在UserInputs類中。 –

+0

它仍然無法正常工作,我將發佈主要的課程,以便您可以完全看到我在做什麼 – nikagar4

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

您的UITextFieldDelegate應該保留在您的RegistrationViewController中。

而不是重寫委託,你可以做到這一點。 在你的init方法中,用適當的函數向自己添加一個目標。

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

這就是我一直在尋找的。 這樣你就不會阻止將來實現MVC的任何視圖,謝謝! :) –

+0

這應該是被接受的答案。它符合OP的要求,並且不會干擾代表的使用。 – Grimxn