Hi, I had a question regarding EnvironmentObject and programmatically switching tabs. I have an EnvironmentObject upon which two views depend and each view has its own tab within a TabView. On the first tab, I intend on having a button that directs the user’s screen directly to the second tab via a selectedTab binding (the code is:
Button {
selectedTab = 1
} label: {
// Some Label
}
with selectedTab being the Binding. However, when it redirects to the secondTab, none of the elements dependent on the environmentObject are loaded in. I have @EnvironmentObject var model: Manager initialized on each of my views and .environmentObject(Manager()) on the launch, so I don’t think it’s the syntax or anything. I think it might be because the user doesn’t click on the second tab so the elements aren’t loaded, but I’m not sure.
Here’s what it looks like after I click the second tab manually:
And here’s what it looks like after I click the button on the first tab without clicking the second tab yet:
Here’s the code that’s supposed to display the first image (Activities View):
VStack (alignment: .leading) {
Text("Schoolwide Events")
.font(.title.weight(.semibold))
.padding(.horizontal)
ScrollView (.horizontal, showsIndicators: false) {
HStack {
ForEach(model.events) {
event in
NavigationLink {
EventDetailView(title: event.title, caption: event.caption, image: event.image, location: event.location, time: event.time, contact: event.contact, description: event.description)
} label: {
HomeViewCard(image: event.image, title: event.title, caption: event.caption, rectangleWidth: (geo.size.width+90)/2, rectangleHeight: (geo.size.width-10)/2)
.padding(.leading)
.padding([.bottom, .top, .trailing], 10)
}
.accentColor(.black)
}
}
}
}
Thanks for the help! I really appreciate it