ITextView的SwiftUI包装器未更新ObservedO

ITextView的SwiftUI包装器未更新ObservedO

本文介绍了用于UITextView的SwiftUI包装器未更新ObservedObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我错了,但是我找不到与可编辑内容等效的SwiftUIUITextView.因此,我使用UIViewRepresentable构建了一个.填充两个SwiftUI文本和我自己的带有ObservableObject的视图都可以工作-但在我的视图中进行的更新是不会传播到ObservableObject.我一定想念一些重要的东西约束力的概念.任何指导将不胜感激.

I hope I'm wrong, but I have not been able to find a SwiftUI equivalent to an editableUITextView. So, I built one using UIViewRepresentable. Populating both a SwiftUI Textand my own view with the ObservableObject works - but updates made in my view arenot propagated to the ObservableObject. I must be missing something important withthe Binding concept. Any guidance would be appreciated.

import SwiftUI
import Combine

struct ContentView: View {

    @ObservedObject var myOText: MyOText

    var body: some View {
        ScrollView {
            VStack {
                Text("This is a bound Text View")
                    .padding(.top, 10)
                    .font(.headline)

                Text(myOText.inTheCourse)
                    .lineLimit(3)
                    .padding()

                Text("This is a multi-line UITextView wrapper:")
                    .font(.headline)

                MultilineTextView(myOText: myOText)
                    .frame(height: 100)
                    .padding()

                Spacer()
            }
        }
    }
}

struct MultilineTextView: UIViewRepresentable {

    @ObservedObject var myOText: MyOText

    func makeUIView(context: Context) -> UITextView {
        let view = UITextView()
        view.isScrollEnabled = true
        view.isEditable = true
        view.isUserInteractionEnabled = true
        view.textAlignment = .center
        view.font = UIFont(name: "Times New Roman", size: 20)
        return view
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = myOText.inTheCourse
    }
}

class MyOText: ObservableObject {
    @Published var inTheCourse: String = "When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them ..."
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(myOText: MyOText())
    }
}

Xcode版本11.2 beta 2(11B44),iOS 13.

Xcode Version 11.2 beta 2 (11B44), iOS 13.

推荐答案

您的多行文本视图需要一个协调器来观察UITextView中的文本更新

Your multiline text view needs a coordinator to observe the text updates from UITextView

struct MultilineTextView: UIViewRepresentable {
    @ObservedObject var myOText: MyOText

    func makeUIView(context: Context) -> UITextView {
        let view = UITextView()
        view.isScrollEnabled = true
        view.isEditable = true
        view.isUserInteractionEnabled = true
        view.textAlignment = .center
        view.font = UIFont(name: "Times New Roman", size: 20)
        view.delegate = context.coordinator
        return view
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = myOText.inTheCourse
    }

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

    class Coordinator: NSObject, UITextViewDelegate {
        var control: MultilineTextView

        init(_ control: MultilineTextView) {
            self.control = control
        }

        func textViewDidChange(_ textView: UITextView) {
            control.myOText.inTheCourse = textView.text
        }
    }
}

这篇关于用于UITextView的SwiftUI包装器未更新ObservedObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:31