SwiftUI UITextView 协调器不起作用 [英] SwiftUI UITextView Coordinator not working

查看:28
本文介绍了SwiftUI UITextView 协调器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIViewRepresentable 中包装了一个 UITextView 并包含一个 Coordinator 作为 UITextViewDelegate,但事件是不叫.我做错了什么?

<预><代码>struct TextView : UIViewRepresentable {typealias UIViewType = UITextViewvar 占位符:字符串@Binding var 文本:字符串func makeCoordinator() ->协调员{协调员(自己)}func makeUIView(context: UIViewRepresentableContext) ->UITextView {让 textView = UITextView()让 placeholderLabel = UILabel()textView.font = UIFont(name: "Helvetica", size: 16)placeholderLabel.restorationIdentifier = "Placeholder"placeholderLabel.text = self.placeholderplaceholderLabel.font = UIFont.italicSystemFont(ofSize: (textView.font?.pointSize)!)placeholderLabel.sizeToFit()textView.addSubview(placeholderLabel)placeholderLabel.frame.origin = CGPoint(x: 25, y: (textView.font?.pointSize)!/2 + 12)placeholderLabel.textColor = UIColor.lightGrayplaceholderLabel.isHidden = !textView.text.isEmpty返回文本视图}func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext) {uiView.text = self.text}类协调器:NSObject,UITextViewDelegate {var 父级:TextView初始化(_ uiTextView:TextView){self.parent = uiTextView}func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) ->布尔{打印(不工作")返回真}func textViewDidChange(_ textView: UITextView) {打印(不工作")}}}

解决方案

必须在 makeUIView 中添加 textView.delegate = context.coordinator 来注册要接收的协调器从 UITextView 更新.

I have wrapped a UITextView in a UIViewRepresentable and included a Coordinator as UITextViewDelegate, but the events are not called. What am I doing wrong?


struct TextView : UIViewRepresentable {
    typealias UIViewType = UITextView

    var placeholder : String

    @Binding var text: String

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: UIViewRepresentableContext<TextView>) -> UITextView {
        let textView = UITextView()
        let placeholderLabel = UILabel()        
        textView.font = UIFont(name: "Helvetica", size: 16)
        placeholderLabel.restorationIdentifier = "Placeholder"
        placeholderLabel.text = self.placeholder
        placeholderLabel.font = UIFont.italicSystemFont(ofSize: (textView.font?.pointSize)!)
        placeholderLabel.sizeToFit()
        textView.addSubview(placeholderLabel)
        placeholderLabel.frame.origin = CGPoint(x: 25, y: (textView.font?.pointSize)! / 2 + 12)
        placeholderLabel.textColor = UIColor.lightGray
        placeholderLabel.isHidden = !textView.text.isEmpty

        return textView
    }

    func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<TextView>) {
        uiView.text = self.text
    }

    class Coordinator : NSObject, UITextViewDelegate {

        var parent: TextView

        init(_ uiTextView: TextView) {
            self.parent = uiTextView
        }

        func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
            print("Not working")
            return true
        }

        func textViewDidChange(_ textView: UITextView) {
            print("Not working")
        }


    }

}


解决方案

You have to add textView.delegate = context.coordinator to makeUIView to register the coordinator to receive updates from UITextView.

这篇关于SwiftUI UITextView 协调器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆