I've just added TabView, which is working fine, but I unable to fetch the selected tab index out if it.
I think I've kept my code perfectly fine, not sure what's wrong.
struct DefaultTabbar: View {
@State private var selectedItem = 1
var body: some View {
TabView(selection: $selectedItem) {
FirstView()
.tabItem{
Image(systemName: "house")
Text("First Tab")
}
.tag(1)
SecondView()
.tabItem{
Image(systemName: "photo")
Text("Second Tab")
}
.tag(2)
}
.accentColor(.orange)
.onTapGesture {
print("selected tab: \(self.selectedItem)")
}
}
}
I'm receiving only 0 while switching tabs.
selected tab: 0.
selected tab: 0.