I have a UIViewControllerRepresentable
struct that is subscribed to an ObservableObject
, like this:
struct ViewControllerWrapper: UIViewControllerRepresentable {
@ObservedObject var chartVM = ChartViewModel()
typealias UIViewControllerType = ViewController
func makeUIViewController(context: Context) -> ViewController {
let lineChartView = LineChartView()
let vc = ViewController(lineChartView: lineChartView)
return vc
}
func updateUIViewController(_ uiViewController: ViewController, context: Context) {
uiViewController.metrics = chartVM.metrics
uiViewController.setChartValues()
}
}
I would like that, when the ObservedObject
changes, either updateUIViewController
is called, or another function that updates the view controller's metrics
array and calls its setChartValues()
method.
Is there a way I can do that? I can't find one.
I can always do it as we used to using only UIKit, but it would be much better to do it using that MVVM pattern.
Help would be much appreciated, thanks!