简体   繁体   English

iOS 14 SwiftUI UIViewRepresentable updateUIView 没有检测到 ObservedObject 的变化?

[英]iOS 14 SwiftUI UIViewRepresentable updateUIView doesn't detect ObservedObject changing?

I have UITextView implemented by UIViewRepresentable.我有 UIViewRepresentable 实现的 UITextView。

struct CustomTextView: UIViewRepresentable {
    var tmpTextVM: TmpTextViewModel
    
    func makeUIView(context: UIViewRepresentableContext<CustomTextView>) -> UITextView {
        let textView = UITextView()
        textView.backgroundColor = UIColor.clear
        textView.isScrollEnabled = false
        textView.text = "aaaaaaaaaaaaaaaaaa"
        textView.font = UIFont(name: "ArialMT", size: 20)
        return textView
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.textColor = tmpTextVM.color
        
    }}

I want to use it like this.我想像这样使用它。

struct ContentView: View {
    
    @ObservedObject var tmpTextVM: TmpTextViewModel
   
        var body: some View {

            VStack {
                
                Button(action: {
                    tmpTextVM.changeColor(UIColor.red)
                }){
                    Image(systemName:"eyedropper.full")
                }
                
                CustomTextView(tmpTextVM: tmpTextVM)
                    .foregroundColor(Color(tmpTextVM.color))
                    .frame(width:300, height: 300, alignment: .topLeading)
                    .border(Color.red, width: 1)
                    
            }
    }
}

On iOS 13 if I tap the button, updateUIView is called and change that text color but on iOS 14 updateUIView isn't called.在 iOS 13 上,如果我点击按钮,则会调用 updateUIView 并更改该文本颜色,但在 iOS 14 上不会调用 updateUIView。 Are there any way to call view update by changing ObservedObject on iOS 14?有没有办法通过更改 iOS 14 上的 ObservedObject 来调用视图更新?

And here codes of TmpTextViewModel and its model.这里是 TmpTextViewModel 及其 model 的代码。

    class TmpTextViewModel: ObservableObject {
    
    @Published private var tmpTextModel: TmpTextModel = TmpTextModel()
    
    var color: UIColor {tmpTextModel.color}
    
    func changeColor(_ color: UIColor) {
        tmpTextModel.color = color
    }
    
}
struct TmpTextModel {
    
   var color:UIColor = UIColor.purple
    
}

You need to make ˋCustomTextViewˋ to listen to changes of ˋTmpTextViewModelˋ.你需要让ˋCustomTextViewˋ来监听ˋTmpTextViewModelˋ的变化。

Just declare ˋtmpTextVMˋ in ˋCustomTextViewˋ as ˋ@ObservedOjectˋ.只需将ˋCustomTextViewˋ中的ˋtmpTextVMˋ声明为ˋ@ObservedOjectˋ。

struct CustomTextView: UIViewRepresentable {
   @ObservedObject var tmpTextVM: TmpTextViewModel
   //....
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS SwiftUI UIViewRepresentable updateUIView 找出哪些属性实际上发生了变化? - iOS SwiftUI UIViewRepresentable updateUIView find out which properties actually have changed? SwiftUI:为什么 ObservedObject 在 AppDelegate 中不起作用? - SwiftUI: Why doesn't ObservedObject work in AppDelegate? SwiftUI: updateUIView() function 在 UIViewRepresentable 导致应用程序冻结和 CPU 峰值 - SwiftUI: updateUIView() function in UIViewRepresentable causes app to freeze and CPU to spike 为什么不更改 @ObservedObject 更新 UIViewRepresentable? - Why don't changes to @ObservedObject update a UIViewRepresentable? SwiftUI - 基于 ObservedObject 的条件在后续视图中不起作用 - SwiftUI - Conditional based on ObservedObject doesn't work in subsequent Views SwiftUI - 尽管使用了@Published 和@ObservedObject,但视图不会更新 - SwiftUI - View doesn't update despite using @Published and @ObservedObject 当传递到视图中的 ObservedObject 更新时,SwiftUI 不会更新? - When an ObservedObject passed into a view is updated, the SwiftUI doesn't update? SwiftUI 不会在嵌套 NavigationLink 目标中使用 ObservedObject 更新 UI - SwiftUI doesn't update UI with ObservedObject in nested NavigationLink destination SwiftUI macOs AppDelegate检测到ObservedObject的变化 - SwiftUI macOs AppDelegate detect the change of ObservedObject SwiftUI 视图在更改 @ObservedObject 时未更新 - SwiftUI view not getting updated on changing @ObservedObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM