Posts

Post not yet marked as solved
5 Replies
3.2k Views
It's my understanding that when a view contains an EnvironmentObject and the object changes in any way, the view should refresh. Is that right?I've got the following view:struct ContentView: View { @EnvironmentObject var globalInfo: appInfo var body: some View { NavigationView { List { ForEach(globalInfo.users.indices, id: \.self) { idx in NavigationLink(destination: UserView(idx: idx)) { Text(self.globalInfo.users[idx]) } } } .navigationBarItems(leading: NavigationLink(destination: AllUserView()) { Text("Show") }, trailing: NavigationLink(destination: NewUserView()) { Text("Add") }) } }}UserView and NewUserView include the same EnvironmentObject declaration and everything seems to be working fine. With breakpoints or using the AllUserView, I can see that changes or additions I make are getting correctly reflected in appInfo, but the list of names in this view never get updated. I've stripped everything out of my app down to this minimal implementation to try to understand the issue but I'm obviously missing something.
Posted
by Rualmpa.
Last updated
.
Post not yet marked as solved
1 Replies
993 Views
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) } }}
Posted
by Rualmpa.
Last updated
.