2

I am building an app with SwiftUI and have login capability that uses Django Rest for authentication. When the user successfully logs in they are taken to the home screen that uses a Tab View. One of the tab views is an account page (Navigation View) that lets the user logout by tapping a button.

When they log out I want it to take them back to the login screen, currently this is done using an if statement and a @State variable to capture when the logout button is pressed:

    @State var logout = false
    var body: some View {
        if (logout) {
            Test()
        } else {
            VStack{
                
                Text("Account View")
                Spacer()
                Spacer()
                Button(action: {
                    self.logout = true
                }, label: {Text("Logout")})
                
            }
        }
    }
}

In this case Test() is the login screen, but when the user gets taken back to this screen the tab bar at the bottom is still showing.

How can I reset the app so that the tab bar doesn't show?

mdav132
  • 125
  • 2
  • 11
  • Could you include the code for the view that includes the `TabView`? I suspect that your login management might need to be determined higher up in the view hierarchy... – ScottM Aug 10 '21 at 10:33

1 Answers1

1

Storing a variable to check wether the user has pressed the logout might not be the very best solution out here. If you want to make login and logout work you should put the views into a NavigationController.

By implementing a NavigationController you can reset to/ call your default Login screen which would not show anything else like the TabView

Asad
  • 279
  • 1
  • 7
  • Apologies I am fairly new to SwiftUI, is a Navigation Controller the same as using a Navigation View? How am I able to reset or call my default login screen? – mdav132 Aug 11 '21 at 03:36
  • 1
    Yes, NavigationController is the same as NavigationView. If your app is going to have a lot of views NavigationController is not the best option for you. You should refer to [this](https://exploringswift.com/blog/using-environmentobject-to-handle-log-in-log-out-condition-in-swiftui) if you have a lot of views. If you don't have a lot of views stick to NavigationController and refer to [this](https://stackoverflow.com/questions/63644579/swiftui-navigation-after-login). And [this](https://stackoverflow.com/questions/57334455/swiftui-how-to-pop-to-root-view) for how to reset back to login. – Asad Aug 11 '21 at 04:27