0

I am simply trying to use the onTapGesture function to change the value of the variable selectedTab when a tab is picked by the user. This onTapGesture function seems to not work because my variable isn't changing value and when I add a print statement that doesn't even execute.

    @Binding var selectedTab: Bool
    @EnvironmentObject var authViewModel: AuthViewModel
    var body: some View {
        TabView(){
            NavigationView(){
                FeedView()
            }
            .tabItem {
                Image(systemName: "h.circle")
                    .onTapGesture {
                        self.selectedTab = false
                    }
            }
ThePyzhov
  • 259
  • 2
  • 14
ahmed
  • 341
  • 2
  • 9
  • What you want is `TabView(selection: $selectedTab) { ... }` – ChrisR Dec 10 '22 at 20:07
  • Hello thanks. I have tried that and its not working, I also get this error in my console "Gesture: System gesture gate timed out." any thoughts – ahmed Dec 10 '22 at 20:18
  • What I suggested saves the value of the selected tab in the State var. You don't need an additional `onTapGesture` as TabView does that automatically. By setting the var in the code you can jump between tabs programmatically. Is that what you want to achieve? – ChrisR Dec 10 '22 at 20:25
  • I want to save the value of selected tab in the var and then check the value of this var in another swift ui view. I used the example added below but I dont know how to call this var in another swift ui view – ahmed Dec 10 '22 at 20:41
  • 1
    @ChrisR never mind got it, all I had to use is a binding. Thanks a lot – ahmed Dec 10 '22 at 21:10

1 Answers1

1

You can receive changing of a tabBar with TabView(selection:), .tag and .onReceive. Also don't forget to import Combine at the top of your file.

Here's an example:

@State private var selectedTab = 1
var body: some View {
    TabView(selection: $selectedTab) {
        NavigationView {
            Text("Navigation view #1")
        }
        .tabItem {
            Image(systemName: "h.circle")
            Text("Home")
        }.tag(1)
        
        NavigationView {
            Text("Navigation view #2")
        }
        .tabItem {
            Image(systemName: "d.circle")
            Text("Home")
        }.tag(2)
    }
    .onReceive(Just(selectedTab)) { _ in
        print("Your text here")
    }
}
ThePyzhov
  • 259
  • 2
  • 14
  • if I wanted to get the value of selected tab in another swift UI view file how would I do this? – ahmed Dec 10 '22 at 20:40
  • nevermind got it, your amazing – ahmed Dec 10 '22 at 21:10
  • @wfrgegeg you can read about passing the data between views here: https://www.createwithswift.com/tutorial-passing-data-between-views-in-swiftui-using-state-and-binding/ Also, if my answer helped you, please mark it as a solution – ThePyzhov Dec 10 '22 at 21:17
  • Hello maybe you can help with this Im struggling. https://stackoverflow.com/questions/76792454/closing-keyboard-causes-matched-geometry-effect-to-execute-swift-ui – ahmed Jul 29 '23 at 18:24