First try in swift. TabView

Hello,

I try to create my first App in Swift. I want to use the TabView. I have 2 fixed tabs button and I want one tab button if has the condition. In the ImprintView, if u push the button, the var will changed but the TabView not updated. If I change the MyVars.isLoggedIn to true on app start, if works, but not if I change via the button.

struct MyVars {
    static var isLoggedIn = false;
}

struct DashboardView: View {
    var body: some View {
        TabView {
            ImprintView()
                .tabItem(){
                    Image(systemName: "house")
                    Text("Imprint")
                }

            AuthLoginFormView()
                .tabItem(){
                    Image(systemName: "camera.fill")
                    Text("Scanner")
                }

            if(MyVars.isLoggedIn){
                RandomView()
                    .tabItem(){
                        Image(systemName: "cube.fill")
                        Text("Random")
                    }
            }
        }
    }
}

struct ImprintView: View {
    var body: some View {
        Button(action: {
            MyVars.isLoggedIn = true
        }) {
            Text("Einloggen")
                .padding(10)
                .frame(maxWidth: .infinity)
                .foregroundColor(.black)
        }
    }
}
Accepted Answer

There are several points in your code.

  • Why declare a static var ?
  • You need to declare an instance of MyVars as a State var in DashBoardView so that view is updated when changed
  • and if you want to change it from another view, you have to declare an instance of MyVars as a Binding var in this other view. Doing so, the MyVars instance in ImprintView will in fact be a pointer to the MyVars var of the DashboardView.

I tested the following which seems to do what you want (I created dummy views for testing purpose):

struct MyVars {
    var isLoggedIn = false
}

struct AuthLoginFormView: View {
    var body: some View {
        Text("AuthLoginFormView")
    }
}

struct RandomView: View {
    var body: some View {
        Text("RandomView")
    }
}

struct DashboardView: View {
    @State private var myVars = MyVars() // It will be initialized to false
    
    var body: some View {
        TabView {
            ImprintView(theVar: $myVars)
                .tabItem(){
                    Image(systemName: "house")
                    Text("Imprint")
                }

            AuthLoginFormView()
                .tabItem(){
                    Image(systemName: "camera.fill")
                    Text("Scanner")
                }

            if myVars.isLoggedIn { // MyVars.isLoggedIn {
                RandomView()
                    .tabItem(){
                        Image(systemName: "cube.fill")
                        Text("Random")
                    }
            }
        }
    }
}

struct ImprintView: View {
    @Binding var theVar: MyVars
    
    var body: some View {
        Button(action: {
            theVar.isLoggedIn = true //  MyVars.isLoggedIn = true
            // To test, you could change this statement with theVar.isLoggedIn.toggle()
        }) {
            Text("Einloggen")
                .padding(10)
                .frame(maxWidth: .infinity)
                .foregroundColor(.black)
        }
    }
}

Note: you can use a more compact declaration for tabItem:

                .tabItem {
                    Label("Imprint", systemImage: "house")
                }
First try in swift. TabView
 
 
Q