I would like to change the value of a text when the active tab of a TabView changes. I tried using onChange(of: activeTab, perform: {})
to change the value of the state variable that stores the text but it seems that the closure given to onChange()
is never called.
TabView(selection: $activeTab) {
ForEach(exampleData) {dayMenu in
DayMenuView(dayMenu: dayMenu)
.padding([.leading, .bottom, .trailing])
}
}
.onChange(of: activeTab, perform: { index in
print(activeTab)
withAnimation {
activeDate = dates[1]
}
})
Text view
Text(activeDate)
State variables
let dates = ["Montag", "Dienstag"]
@State private var activeTab: Int = 0
@State private var activeDate = "Montag"