I would like to run a function each time a tab is tapped.
On the code below (by using onTapGesture
) when I tap on a new tab, myFunction
is called, but the tabview is not changed.
struct DetailView: View {
var model: MyModel
@State var selectedTab = 1
var body: some View {
TabView(selection: $selectedTab) {
Text("Graphs").tabItem{Text("Graphs")}
.tag(1)
Text("Days").tabItem{Text("Days")}
.tag(2)
Text("Summary").tabItem{Text("Summary")}
.tag(3)
}
.onTapGesture {
model.myFunction(item: selectedTab)
}
}
}
How can I get both things:
- the tabview being normally displayed
- my function being called