在 Xcode 中设置自定义 UITextView 委托的委托

Setting the delegate of a custom UITextView Delegate in Xcode

我有一个 class 可以创建自定义 UITextView 并使用提示文本对其进行初始化。我遇到的问题是,当我尝试在初始化程序中设置 UITextView 的委托时,不会调用委托函数。我也不完全确定 textView class 是否应该是代表。 下面是自定义 UITextView class:

class TextView: UITextView, UITextViewDelegate {
    var test: TextView?
    var hintLabel, smallHintLabel: UILabel!
    var underline: UIView!

    // MARK: - Init
    
    /// Initialize text field
    /// - Parameters:
    ///   - text: Text field text
    ///   - hintText: Text field hint text (a.k.a. placeholder/description)
    ///   - isLight: Whether text field is light style
    convenience init(text: String? = nil, hintText: String, isLight: Bool = false) {
        self.init(frame: .zero, textContainer: .none)
        
        self.heightAnchor.constraint(equalToConstant: 57).isActive = true
        //setting the delegate
        test = TextView()
        self.delegate = test
        
        // hint
        hintLabel = UILabel()
        hintLabel.text = hintText
        hintLabel.font = UIFont.systemFont(ofSize: largeSize, weight: weight)
        hintLabel.textColor = hintColor
        hintLabel.alpha = text == nil || text!.isBlank ? 1 : 0
        self.addSubview(hintLabel)
        
        hintLabel.translatesAutoresizingMaskIntoConstraints = false
        hintLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: insets.top / 2).isActive = true
        hintLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
        hintLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
        
        // small hint
        smallHintLabel = UILabel()
        self.smallHintLabel.text = hintText
        self.smallHintLabel.font = UIFont.systemFont(ofSize: smallSize, weight: weight)
        self.smallHintLabel.textColor = hintColor
        self.smallHintLabel.alpha = text == nil || text!.isBlank ? 0 : 1
        self.addSubview(smallHintLabel)
        
        smallHintLabel.translatesAutoresizingMaskIntoConstraints = false
        smallHintLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        smallHintLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
        smallHintLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
        
        // text
        self.text = text
        self.font = UIFont.systemFont(ofSize: largeSize, weight: weight)
        self.textColor = textColor
        self.tintColor = isLight ? .white : Color.blue
        self.keyboardAppearance = isLight ? .dark : .light
        
        // underline
        underline = UIView()
        underline.backgroundColor = lineColor
        self.addSubview(underline)
        
        underline.translatesAutoresizingMaskIntoConstraints = false
        underline.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        underline.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
        underline.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
        underline.heightAnchor.constraint(equalToConstant: 1).isActive = true
        
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

/// Adjusts the position of the hint text when text is changed in field
    func textViewDidChange(_ textView: UITextView) {
        print(textView.text)
        if self.text?.isEmpty ?? true {
            moveHintUp()
        } else {
            moveHintUp()
        }
    }

我正在创建此函数的实例并将其添加到视图中。


class MessageComposeView: UIView, UITextViewDelegate {
    
    var textField: TextView!
    private var sendButton: SendButton!
    
    weak var delegate: MessageComposeViewDelegate?

    init() {
        super.init(frame: .zero)
        
        // background
        
        // text field
        textField = TextView(hintText: "Type a message")
        textField.delegate = self
        self.addSubview(textField)
        
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.topAnchor.constraint(equalTo: self.topAnchor, constant: 30).isActive = true
        textField.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -30).isActive = true
        textField.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 16).isActive = true
        textField.heightAnchor.constraint(equalToConstant: 50).isActive = true
        textField.sizeToFit()
        
        // send button
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    // MARK: - Actions
    
    /// Called when the message text changes
    func textViewDidChange(_ textView: UITextView) {
        sendButton.setState(textField.text?.isBlank == true ? .disabled : .normal)
        
        let size = CGSize(width: 200, height: 999999)
        let estimatedSize = textField.sizeThatFits(size)

        textField.constraints.forEach { constraint in
            if constraint.firstAttribute == .height {
                constraint.constant = estimatedSize.height
            }
        }
    }

我的猜测是你实际上并没有调用你想要的初始化器。

例如,如果您调用 TextView(),您的 convenience init 将不会被调用 。但是,如果您调用 TextView(hintText: "") 或您创建的初始值设定项的任何其他变体,它将执行。

其次,虽然它与上述问题没有直接关系,但我质疑您为什么要创建 另一个 TextView 并将其指定为委托。为什么不直接将委托分配给 self