简体   繁体   English

SWIFTUI 按钮登录认证和 go 到新视图

[英]SWIFTUI button login authentication and go to new View

I ned easy validation TextField after enter Email and password, next step push button, check Email and Password and go to next View.在输入 Email 和密码后,我需要简单的验证 TextField,下一步按钮,检查 Email 和密码以及 go 到下一个视图。 How it make?它是如何制作的? I have a some code State var and Button:我有一些代码 State var 和 Button:

struct ContentView: View {

    @State var email = "1"
    @State var password = "1"

    Button(action: {
              if self.email == "1" && self.password == "1"{
              print("Button tapped")
              NextMyView()
              }else {
           print("error")
                            }
                        }) {
                            Image(systemName: "heart.fill")
                                .foregroundColor(.red)
                        }

But my idea dos not work, how I can make this validation and after push button go to next View.但是我的想法不起作用,我如何进行此验证并在将 go 按钮按下到下一个视图之后。

You will need NavigationView and a NavigationLink to achieve what you're looking for.您将需要NavigationViewNavigationLink来实现您正在寻找的内容。

NavigationView will wrap all the inner views within a navigation controller, and NavigationLink is like a special button that triggers the navigation. NavigationView会将所有内部视图包装在一个导航 controller 中, NavigationLink就像一个触发导航的特殊按钮。

Example:例子:

struct ContentView: View {
  @State private var email: String = "root"
  @State private var password: String = "toor"
  
  @State private var isLoginValid: Bool = false
  @State private var shouldShowLoginAlert: Bool = false
  
  var body: some View {
    NavigationView {
      VStack(alignment: .center) {
        TextField("email", text: self.$email)
        TextField("password", text: self.$password)
        
        NavigationLink(destination: Text("Success"),
                       isActive: self.$isLoginValid) {
                /*
                 Here we put the content view of `NavigationLink`.
                 It could be any `View` even `Button` but in this
                 example we use a `Text` with `onTapGesture`.
                 */
                Text("Login")
                    .onTapGesture {
                    //determine login validity
                    let isLoginValid = self.email == "root" && self.password == "toor"
                    
                    //trigger logic
                    if isLoginValid {
                      self.isLoginValid = true //trigger NavigationLink
                    }
                    else {
                      self.shouldShowLoginAlert = true //trigger Alert
                    }
                }
        }
      }
      .navigationBarTitle("Login Screen")
      .alert(isPresented: $shouldShowLoginAlert) {
        Alert(title: Text("Email/Password incorrect"))
      }
    }
  }
}

NavigationLink defines: NavigationLink定义:

  1. destination which navigates to a simple Text view导航到简单Text视图的destination
    • In your case you should change it to NextMyView()在您的情况下,您应该将其更改为NextMyView()
  2. isActive binded to a boolean value loginSuccess which when updated to true shall re-render the view and cause NavigationLink to fire automatically isActive绑定到 boolean 值loginSuccess ,当更新为true时应重新渲染视图并导致NavigationLink自动触发
  3. The content view is a simple Text with a onTapGesture in which we can implement the success/failure case handling.内容视图是一个带有onTapGesture的简单Text ,我们可以在其中实现成功/失败案例处理。
    • In your case, you could change Text to Image(systemName: "heart.fill")在您的情况下,您可以将Text更改为Image(systemName: "heart.fill")

PS: As an extra, I included the logic to trigger and show an alert on login failure. PS:作为额外的,我包含了触发和显示登录失败警报的逻辑。
Hope it helps:)希望能帮助到你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM