UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to check which tab the user is on?

Forums > SwiftUI

Hi all, So i have three tabs A, B and C and inside C theres is a menu. My goal is to close that menu whenever i change tabs. How can i do this? Another way of asking is, how can i check to see what tab the user is on?

  • Nick G

3      

I think @Environment presentationMode

then u can write states for each view and by switching the views get which one is active from environment

3      

Thanks for the answer, im not very experienced would it be possible for you to explain to me what the code would look like?

3      

I tried doing something like this

 DiscoverView().onTapGesture {
                    print("HELLO")
                }
                .tabItem({
                    Image(systemName: "globe")
                        .font(.system(.title))
                    Text("Discover")
                }).tag(0)

But it doesnt print "hello" when the tab is clicked

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.