Ryann786
Ryann786

Reputation: 309

NavigationLink Swift UI Not showing View

When the App loads and it's checked if the user is already logged in, I want to navigate to the ListView without showing another view first where the user has to press some text to view the list.

Text("Why do I need this? Why not just show the view?")- Why is this needed? Why can't I just push to the ListView()? import SwiftUI

struct ContentView: View {


    //MARK: Properties
    @ObservedObject var session = FirebaseSession()

    @State var selection: Int? = nil

    var body: some View {

        NavigationView {
            Group {
                if session.session != nil {
                    VStack {
                        ListView()
                        NavigationLink(destination: ListView(), tag: 1, selection: $selection) {
                            Text("Why do I need this? Why not just show the view?")
                        }
                    }
                } else {
                    LoginView()
                }
            }
            .onAppear(perform: getUser)
            .padding()
        }
    }

    //MARK: Functions
    func getUser() {
        session.listen()
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 1

Views: 479

Answers (1)

UIChris
UIChris

Reputation: 621

If I understand your question correctly, you want to have a specific view first if you user is already logged in.

Why don't you perform your getUser() method within the SceneDelegate, and depending on the user's state, you set the first view, like so :

if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)

        var firstView = session.session != nil ? MainView() : LoginView()
        window.rootViewController = UIHostingController(rootView: firstView)

        self.window = window
        window.makeKeyAndVisible()
    }

Hope this helps man!

Upvotes: 2

Related Questions