I am having my main view in ContentView class and I do the network call in ContentViewModel class and update a class call User with the response.
My ContentView has several subviews such as HeaderView. When I update the User class properties from the API response, I want to update UI elements defined in HeaderView as well. How can I pass data to my subview (HeaderView) from the ContentViewModel class?
I can update the ContentView elements which is pretty simple by setting the ContentViewModel class ObservableObject
class ContentViewModel: ObservableObject {
and Published the User object
@Published var user = User()
and update the attribute of user once I received the API response in the same ContentViewModel
self.user.name = name
where my ContentView has the relationship with it's model class like below
@StateObject private var contentViewModel = ContentViewModel()
and the UI updates as follow
Text("My name \(contentViewModel.user.name)")
That is the working scenario for ContentView. But I need to pass the value to HeaderView(). My user class is like below
import Foundation
class User {
var name = ""
// more attributes
}