I'm still pretty new to swift and swiftui but this seems pretty basic.
Two TextField/Button pairs - the goal is to disable the button when the corresponding text field is empty. Only difference between the sets is that one uses a String while the other uses a String within a class. I've tried it with and without the ObserveableObject and @Published declarations.
If I type in the first text field, the button lights up as expected. In the second, nothing. Huh?
class User: ObservableObject {
@Published var name: String
init() {
name = ""
}
}
struct ContentView: View {
@State var user = User()
@State var username = ""
var body: some View {
VStack {
TextField("New Name", text: $username)
Button(action: {print(self.username)}) {Text("String")}.disabled(self.username.isEmpty)
TextField("New Name", text: $user.name)
Button(action: {print(self.user.name)}) {Text("User Class")}.disabled(self.user.name.isEmpty)
}
}
}