Cannot switch views through @State binding variable in SwiftUI

118 views Asked by At

I'm trying to pass a state variable between view files in order to navigate between views, but it doesn't seem to work. The "view" variable is updated when the register button is pressed, but it doesn't change the view. This is a simplified version of how I am implementing it currently:

import SwiftUI
import FireBaseAuth

struct ContentView: View {
    @State private var view = "Main"
    
    var body: some View {
        
        if Auth.auth().currentUser?.uid == nil {
            LoginView(view: $view)
        }
        else if view == "Register" {
            RegisterView(view: $view)
        }
        else {
            MainView()
        }
    }
}

LoginView

import SwiftUI

struct LoginView: View {
    @Binding var view: String
    var body: some View {
        
        return VStack(content: {
            
            Button(
                action: {
                    view = "Register"
                    print(view)
                },
                label: {
                    Text("Register")
                })
            
        })
        
    }
}

RegisterView

import SwiftUI

struct RegisterView: View {
    @Binding var view: String
    var body: some View {
        Text("Hi!")
        Button("Back", action: {
            view = "Login"
        })
    }
}
0

There are 0 answers