简体   繁体   中英

UIViewRepresentable view not updating value

I am trying to create a custom SwiftUI View but I am getting problem in updating value through 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

Edit: I changed code after @Asperi answer now it is updating value. but as Dávid Pásztor suggested it is not right to create @ObservedObject inside struct then how I will pass the value to CustomView with creating @ObservedObject and link that David provided is old and code in that thread now seems not working.

Here is the code that is working

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)")
            }
        }
        
        
    }
}

You don't need to keep it as property because struct can be recreated (so you loose it), instead you have access your view via provided in arguments.

Note: UIViewRepresentable handles reference to corresponding view by itself.

struct CustomView: UIViewRepresentable {
    typealias UIViewType = UILabel

    var value:String

    func makeUIView(context: Context) -> UILabel {
        let lblTitle = UILabel()
        lblTitle.text = value
        lblTitle.backgroundColor = UIColor.red
        lblTitle.textColor = UIColor.black
        return lblTitle
    }
    
    func updateUIView(_ uiLabel: UILabel, context: Context) {
        uiLabel.text = value
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM