【问题标题】:Button click go another page in swiftui按钮单击转到swiftui中的另一个页面
【发布时间】:2021-12-07 22:25:34
【问题描述】:

我有两个视图,一个是 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 ")
       }
      }
    

我想在 clcik 按钮上转到另一页并返回上一个 onclick 按钮。请帮忙。

【问题讨论】:

标签: swiftui swiftui-navigationview


【解决方案1】:

您可以尝试在您的按钮上单击 LoginView 以从 LoginView 转到 WelcomeView:

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)
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-12-21
    • 2011-12-15
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 2015-10-16
    • 2021-09-07
    相关资源
    最近更新 更多