TabBar

In my SwiftUI app, I have a TabBar with 2 items:
a) TabItem1
b) TabItem2

However on the View itself, I created 2 buttons:
a) Button A
b) Button B

Now I need to switch TabBar items, using the buttons on the View. How can I do this ?
  • *******

Button(action: {
//call tabItem 1
 })
{
    Text("TabItem1")
}

Button(action: {
//call tabItem 2
 })
{
    Text("TabItem2")
}

****************************
     
TabView {
      TabView1View()
        .tabItem {
          Text("TabItem1")
        }
       
      TabView2View()
        .tabItem {
          Text("TabItem2")
        }

Thank you
Accepted Answer

Now I need to switch TabBar items, using the buttons on the View. How can I do this ?

You may need to access the selection of TabView:
Code Block
struct ContentView: View {
@State var tabSelection: Int = 1
var body: some View {
TabView(selection: $tabSelection) {
TabView1View(tabSelection: $tabSelection)
.tabItem {
Text("TabItem1")
}
.tag(0)
TabView2View(tabSelection: $tabSelection)
.tabItem {
Text("TabItem2")
}
.tag(1)
}
}
}
struct TabView1View: View {
@Binding var tabSelection: Int
var body: some View {
Button(action: {
//call tabItem 2
self.tabSelection = 1
})
{
Text("TabItem1")
}
}
}
struct TabView2View: View {
@Binding var tabSelection: Int
var body: some View {
Button(action: {
//call tabItem 1
self.tabSelection = 0
})
{
Text("TabItem2")
}
}
}


TabBar
 
 
Q