Im trying to follow along in a tutorial and I am having a problem with the action after clicking the button. To debug, I cleaned my code of everything apart from the action itself and it still doesn't work... Could you help? Its probably something simple:
import Foundation
class UserInfo: ObservableObject{
@Published var test = "test"
}
And the view:
import SwiftUI
import Firebase
struct ContentView: View {
@StateObject var userInfo: UserInfo = UserInfo()
var body: some View {
VStack{
Text(userInfo.test)
Button(action: {
self.userInfo.test = "passed"
}) {
Text("Log in")
}
.font(.title)
.padding(5)
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.top, 10)
}
}
}
Above code will work fine. But when I try to make new view:
struct NewView: View {
@StateObject var userInfo: UserInfo = UserInfo()
var body: some View {
VStack{
Text("test")
Button(action: {
self.userInfo.test = "passed"
}) {
Text("Log in")
}
.font(.title)
.padding(5)
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.top, 10)
}
}
}
And than I put it into Content View:
struct ContentView: View {
@StateObject var userInfo: UserInfo = UserInfo()
var body: some View {
NewView()
}
}
The button stops working... I assume that both of those views conform to the ObservableObject. When it is changed ContentView should update, but nothing happens.