簡體   English   中英

UIViewRepresentable 視圖不更新值

[英]UIViewRepresentable view not updating value

我正在嘗試創建自定義 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)")
    }
}

項目可以從這里下載

編輯:我在@Asperi 回答之后更改了代碼,現在它正在更新值。 但正如 Dávid Pásztor 所建議的那樣,在 struct 中創建 @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)")
            }
        }
        
        
    }
}

您不需要將其保留為屬性,因為 struct 可以重新創建(因此您可以丟失它),相反您可以通過參數中提供的方式訪問您的視圖。

注意: UIViewRepresentable處理對相應視圖的引用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM