I'm trying to show a dialog over ContentView. The dialog view, ShowDialogView, has an ObservedObject object with name and properties.
class User: ObservableObject {
@Published var name = ""
@Published var age: Int = 0
}
struct ShowDialogView: View {
@Binding var isPresented: Bool
@ObservedObject var user: User
/*
@State var name = ""
*/
init(isPresented: Binding<Bool>, user: User) {
self._isPresented = isPresented
self.user = user.searchWord
//_name = State(initialValue: "Kimberly")
}
var body: some View {
VStack {
...
...
}.onAppear {
print("\(user.name)")
}
}
}
struct ContentView: View {
@State var user = User()
@State var showMe = true
var body: some View {
VStack {
...
...
ShowDialogView(isPresented: showMe, user: user)
}
}
}
The dialog view will open with no problem. The problem that I have is that the user object doesn't deliver anything beyond default values. If I somehow set the name property to "Kimberly" before the dialog appears, the app will end up showing no value (""). Even if I try setting an initial value to the name property in the constructor, the app will still show an empty value. What am I doing wrong? I'm sorry I cannot give you a lot of details in the code above. Thank you.