1

I have two view one is LoginView. Another is WelcomeView. I want to go from LoginView to WelcomeView onclick button from LoginView.

Here is my code..

  1. LoginView

    struct LoginView: View {
    
     var body: some View {
       VStack{
        Button(action: {
            print("*** Press go login view ****")
    
        }) {
            Text("Login")
                .font(.custom(TextConstant.keyValues.front_name, size: 30))
                .foregroundColor(.white)
                .fontWeight(.bold)
                .frame(minWidth: 0, maxWidth: .infinity)
                .padding(.all,20)
                .foregroundColor(.blue)
                .background(LinearGradient(gradient: Gradient(colors: [.green, .green]), startPoint: .leading, endPoint: .trailing))
                .cornerRadius(10)
          }
         }
       }
     }
    
  2. Here is WelcomeView

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

I want to go another page on clcik button & back to previous onclick button. Please help.

Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
  • 2
    Does this answer your question? [How to navigate between screens using a button in SwiftUI](https://stackoverflow.com/questions/67420653/how-to-navigate-between-screens-using-a-button-in-swiftui) – mahan Oct 21 '21 at 07:26

1 Answers1

4

you could try this to go from LoginView to WelcomeView on your button click in LoginView:

struct LoginView: View {
    @State private var showWelcomeView = false
    
    var body: some View {
        NavigationView {
            VStack {
                Button(action: { showWelcomeView = true }) {
                    Text("Login")
                        .font(.custom(TextConstant.keyValues.front_name, size: 30))
                        .foregroundColor(.white)
                        .fontWeight(.bold)
                        .frame(minWidth: 0, maxWidth: .infinity)
                        .padding(.all,20)
                        .foregroundColor(.blue)
                        .background(LinearGradient(gradient: Gradient(colors: [.green, .green]), startPoint: .leading, endPoint: .trailing))
                        .cornerRadius(10)
                }
                NavigationLink("", destination:  WelcomeView(), isActive: $showWelcomeView)
            }
        }
    }
}