Simplest example below. Works fine in preview (UITextView
text updates to "ouch"). But, run it in an app (ie add as rootView
by the sceneDelegate
), and the UITextView
doesn't update.
import SwiftUI
class ModelObject : ObservableObject{
@Published var text = "Model Text"
}
struct MyTextView : UIViewRepresentable {
@ObservedObject var modelObject : ModelObject
func makeUIView(context: Context) -> UITextView {
let result = UITextView()
result.isEditable = true
return result
}
func updateUIView(_ view: UITextView, context: Context) {
view.text = modelObject.text
}
}
struct BugDemoView : View{
@ObservedObject var modelObject : ModelObject
var body : some View{
VStack{
MyTextView(modelObject: modelObject)
Button(action: {
self.modelObject.text = "ouch"
}){
Text("Button")
}
}
}
}
#if DEBUG
var mo = ModelObject()
struct BugDemoView_Preview: PreviewProvider {
static var previews: some View {
BugDemoView(modelObject: mo)
}
}
#endif
using Combine
andobjectWillChange
calls - and I didn't get it to work either. What I did get working was "old school" - postingNotification
that my representable subscribed too. hopefully one of the gurus has a better solution, but since I asked virtually the same thing almost two months ago, I say just use what works. – Cohleen@ObservedObject
doesn't updates myUIViewControllerRepresentable
. – Forewing