UIViewRepresentable视图不更新值

时间:2020-09-17 12:44:03

标签: ios swift swiftui combine uiviewrepresentable

我正在尝试创建自定义的SwiftUI视图,但是在通过ObservedObject更新值时遇到问题。

import SwiftUI
import Combine

struct ContentView: View {
    @ObservedObject var model:InfoViewModel = InfoViewModel()
    var body: some View {
        VStack(alignment: .leading){
            CustomView(value: self.model.title)
                .frame(width: 200, height: 200, alignment: .center)
        }
        .background(Color.green)
        .frame(minWidth: 0,
               maxWidth: .infinity,
               minHeight: 0,
               maxHeight: .infinity,
               alignment: .topLeading)
    }
}

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

struct CustomView: UIViewRepresentable {
    typealias UIViewType = UIView
    var value:String
    var lblTitle:UILabel = UILabel()
    func makeUIView(context: Context) -> UIView {
        let view:UIView = UIView()
        view.addSubview(lblTitle)
        lblTitle.text = value
        lblTitle.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        lblTitle.backgroundColor = UIColor.red
        lblTitle.textColor = UIColor.black
        return view
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
        self.lblTitle.text = value
        print("LBLTitle:\(self.lblTitle.text!)\nTitleValue:\(self.value)")
    }
}


class InfoViewModel: ObservableObject, Identifiable {
    @Published var title = "Title"
    private var count = 0
    init() {
        _ = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
    }
    
    @objc func fireTimer() {
        count += 1
        
        self.title = "\(self.count)"
        print("Timer fired!\n\(self.title)")
    }
}

Project can be download from here

编辑: 我在@Asperi回答后更改了代码,现在它正在更新值。但是正如DávidPásztor所建议的那样,在结构内部创建@ObservedObject是不正确的,那么我将如何通过创建@ObservedObject并将该值传递给CustomView以及David提供的链接已经很旧,并且该线程中的代码现在似乎无法正常工作。

这是有效的代码

struct CustomView: UIViewRepresentable {
    typealias UIViewType = UIView
    var value:String
    
    func makeUIView(context: Context) -> UIView {
        let view:UIView = UIView()
        let lblTitle:UILabel = UILabel()
        view.addSubview(lblTitle)
        lblTitle.text = value
        lblTitle.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        lblTitle.backgroundColor = UIColor.red
        lblTitle.textColor = UIColor.black
        return view
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
        for view in uiView.subviews
        {
            if(view.isKind(of: UILabel.self))
            {
                (view as! UILabel).text = value
                print("LBLTitle:\((view as! UILabel).text!)\nTitleValue:\(self.value)")
            }
        }
        
        
    }
}

1 个答案:

答案 0 :(得分:0)

您无需将其保留为属性,因为可以重新创建struct(因此可以松开它),而可以通过参数中提供的方式访问视图。

注意:false自己处理对相应视图的引用。

UIViewRepresentable