Home > Software engineering >  How to push screen programmatically on a condition in swiftui?
How to push screen programmatically on a condition in swiftui?

Time:12-29

I am trying to to push screen2 after dismissing a custom pop up alert. I am showing a login button on the first screen on click of login button I am showing a custom pop up alert which has one OK button. On click of ok button I am dismissing the pop up alert and trying to push screen2 but I don't have any idea how would I push the screen2.

ContentView

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            return NavigationView {
                LoginView()
            }
        }
        .padding()
    }
}

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

LoginView

import SwiftUI

struct LoginView: View {
    @State var isConfirmDialogPresented = false
    @State var isButtonPressed = false
    var body: some View {
        ZStack {
            Color.white
                .ignoresSafeArea()
            VStack {
                Button {
                    isConfirmDialogPresented = true
                } label: {
                    Text("Login")
                }

            }
           
            if isConfirmDialogPresented {
                CustomAlertView(isButtonPressed: $isButtonPressed)
            }
            
            if isButtonPressed {
                //go to screen2
            }
            
        }
        
    }
}

struct LoginView_Previews: PreviewProvider {
    static var previews: some View {
        LoginView()
    }
}

Custom Pop Up alert

import SwiftUI

struct CustomAlertView: View {
    @Binding var isButtonPressed: Bool
    var body: some View {
        ZStack {
            VStack {
                Button {
                    isButtonPressed = true
                } label: {
                    Text("OK")
                        .foregroundColor(.white)
                        .padding()
                }
                .background(.red)

            }.frame(width: 340, height: 320)
            .background(Color.cyan)
        }
    }
}

struct CustomAlertView_Previews: PreviewProvider {
    static var previews: some View {
        CustomAlertView(isButtonPressed: .constant(false))
    }
}

Screen2

import SwiftUI

struct Screen2: View {
    var body: some View {
        Text("Welcome to Screen2")
    }
}

struct Screen2_Previews: PreviewProvider {
    static var previews: some View {
        Screen2()
    }
}

CodePudding user response:

You need to pull the condition up to ContentView and decide in there what you want to show. E.g.:

struct ContentView: View {
    @State private var showLogin: Bool = true
    var body: some View {
        VStack {
            if showLogin{
                NavigationView {
                    LoginView(showLogin: $showLogin)
                }
            } else{
                Screen2()
            }
        }
        .padding()
    }
}

And use this var in LoginView and your CustomAlert to trigger it.

struct LoginView: View {
    @State private var isConfirmDialogPresented = false
    @Binding var showLogin
    var body: some View {
        ZStack {
            Color.white
                .ignoresSafeArea()
            VStack {
                Button {
                    isConfirmDialogPresented = true
                } label: {
                    Text("Login")
                }

            }
           
            if isConfirmDialogPresented {
                CustomAlertView(showLogin: $showLogin)
            }
            
        }
        
    }
}

struct CustomAlertView: View {
    @Binding var showLogin: Bool
    var body: some View {
        ZStack {
            VStack {
                Button {
                    showLogin = false
                } label: {
                    Text("OK")
                        .foregroundColor(.white)
                        .padding()
                }
                .background(.red)

            }.frame(width: 340, height: 320)
            .background(Color.cyan)
        }
    }
}

CodePudding user response:

You have to add some conditions in loginView. Check below code

import SwiftUI

struct LoginView: View {
    @State var isConfirmDialogPresented = false
    @State var isButtonPressed = false
    var body: some View {
        ZStack {
            Color.white
                .ignoresSafeArea()
            VStack {
                Button {
                    isConfirmDialogPresented = true
                } label: {
                    Text(isButtonPressed ? "":"Login")
                }

            }
           
            if isConfirmDialogPresented && !isButtonPressed {
                CustomAlertView(isButtonPressed: $isButtonPressed)
            }
            
            if isButtonPressed {
                Screen2()
            }
            
        }
        
    }
}

struct LoginView_Previews: PreviewProvider {
    static var previews: some View {
        LoginView()
    }
}

or

you have to add the navigation on screen2.

Remove return in contentView

struct ContentView: View {
    var body: some View {
            VStack {
                NavigationView {
                    LoginView()
                }
            }
            .padding()
        }
}

in loginView

struct LoginView: View {
    @State var isConfirmDialogPresented = false
    @State var isButtonPressed = false
    var body: some View {
        ZStack {
            Color.white
                .ignoresSafeArea()
            VStack {
                Button {
                    isConfirmDialogPresented = true
                } label: {
                    Text("Login")
                }

            }
           
            if isConfirmDialogPresented{
                CustomAlertView(isButtonPressed: $isButtonPressed)
            }
            
            NavigationLink(destination: Screen2(), isActive: $isButtonPressed) {
                EmptyView()
            }
        }
        
    }
}

CodePudding user response:

Something like this has already been answered, check the link as it might be helpful;

SwiftUI NavigationLink doesn't trigger with onReceive method

  • Related