从didSet

时间:2018-06-05 02:21:26

标签: ios swift delegates uitextfield

我有一个UITextField的子类,用于在特定的textField事件之后调用两个方法。 textField使用委托/协议设置调用textField的超类中的方法。

class SearchTermCellTextField: UITextField {

    var searchTermCellTextFieldDelegate: SearchTermCellTextFieldDelegate?

    override var text: String? {
        didSet {
            searchTermCellTextFieldDelegate?.textFieldDidChange(self)
        }
    }

    override func deleteBackward() {
        super.deleteBackward()
        searchTermCellTextFieldDelegate?.textFieldReceivedDelete(self)
    }

}

protocol SearchTermCellTextFieldDelegate {

    func textFieldReceivedDelete(_ textField: UITextField)

    func textFieldDidChange(_ textField: UITextField)

}

正确初始化委托,并且textFieldReceivedDelete()被调用没有问题。但是,每次调用textFieldDidChange()时(var text { didSet }),searchTermCellTextFieldDelegate的值都为零。

我尝试将searchTermCellTextFieldDelegate?.textFieldDidChange()放入didSet调用的辅助方法中,以查看它是否会产生任何影响,但仍然相同。我在deleteBackward()内测试了它 - 我不想要它 - 并且searchTermCellTextFieldDelegate?具有预期的非零值,并且它可以很好地发射。

我唯一的猜测是文本变量{ get set }中发生了一些更复杂的事情导致问题,但除此之外我不知道从哪里开始解决问题。

有没有人有任何见解?提前谢谢。

1 个答案:

答案 0 :(得分:1)

原因 - 你覆盖了一个textField的文本,但OP想要触发的函数textFieldDidChange基于编辑已更改的控件事件,因此在您的情况下不需要覆盖text,因为文本不会是在更改或编辑其值时触发

因此,要使该类委托工作,您实际上需要密切关注编辑TextField的更改控制事件

试试这个

import Foundation
import UIKit


class SearchTermCellTextField: UITextField {

    var TFDelegate : SearchTermCellTextFieldDelegate?

    /// Initialise class
    override init(frame: CGRect) {
        super.init(frame: frame)
        /// Need to add a target to notify class that text did changed
        self.addTarget(self, action: #selector(SearchTermCellTextField.editingChanged), for: .editingChanged)
    }

    /// Coder
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// You override Text Not its editing changed
    override var text: String? {
        didSet {
            TFDelegate?.textFieldDidChange(self)
        }
    }

    /// Handler which tells that editing has been changed
    @objc func editingChanged(){
        TFDelegate?.textFieldDidChange(self)
    }

    /// Its Working
    override func deleteBackward() {
        super.deleteBackward()
        TFDelegate?.textFieldReceivedDelete(self)
    }

}

protocol SearchTermCellTextFieldDelegate {

    func textFieldReceivedDelete(_ textField: UITextField)

    func textFieldDidChange(_ textField: UITextField)

}

ViewController类

import UIKit

class ViewController: UIViewController {
    private var myFirstTF : SearchTermCellTextField?
}

/// View Life cycles
extension ViewController
{
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        myFirstTF = SearchTermCellTextField()
        myFirstTF?.TFDelegate = self
        myFirstTF?.placeholder = "Enter Text"

        myFirstTF?.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(myFirstTF!)
        myFirstTF?.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20).isActive=true
        myFirstTF?.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 20).isActive=true
        myFirstTF?.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive=true
        myFirstTF?.heightAnchor.constraint(equalToConstant: 50)

    }
}

extension ViewController: SearchTermCellTextFieldDelegate
{    
    func textFieldReceivedDelete(_ textField: UITextField) {
        print("delete Recieved")
    }

    func textFieldDidChange(_ textField: UITextField) {
        print("Text:\(textField.text ?? "Empty")")
    }
}

控制台输出

enter image description here