按钮单击转到 swiftui 中的另一页

Button click go another page in swiftui

我有两个视图,一个是 LoginView。另一个是 WelcomeView。我想从 LoginView 转到 WelcomeView onclick button from LoginView。

这是我的代码..

  1. 登录视图

    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. 这里是 WelcomeView

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

我想通过单击按钮转到另一页并返回到上一个单击按钮。请帮忙

你可以试试这个从 LoginViewWelcomeView 点击你的按钮 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)
            }
        }
    }
}