为什么我的@State不会更改我的条件语句?

时间:2019-11-05 09:02:36

标签: swift if-statement swiftui

我有一条条件语句,可以向用户显示@State以便登录或注册。当我单击SignIn $ SignUpButton时,按钮的@State会更改,但我的条件语句不会触发。

有什么遗漏吗?

struct CredentialsView: View {

  @State var signInSelected = true
  @State var signUpSelected = false

  @State var firstName = ""
  @State var email = ""
  @State var password = ""
  @State var confirmPassword = ""

  var body: some View {
    VStack {
      SignIn$SingUpButton(signInSelected: signInSelected,
                          signUpSelected: signUpSelected
      )

      VStack(spacing: 30) {
        if signInSelected {
          UserEntryTextField(title: "email", userEntry: email)
          UserEntryTextField(title: "password", userEntry: password)
        }
        else if signUpSelected {
          UserEntryTextField(title: "first name", userEntry: firstName)
          UserEntryTextField(title: "email", userEntry: email)
          UserEntryTextField(title: "password", userEntry: password)
          UserEntryTextField(title: "confirm Password", userEntry: confirmPassword)
        }
      }
      .padding(.top, signInSelected ? 110 : 50)
      Spacer()
    }
    .padding(.vertical, 40)
  }
}

2 个答案:

答案 0 :(得分:1)

您必须使用@Binding才能实现。查看有关绑定的示例,您会更清楚地了解

答案 1 :(得分:0)

我必须更改一些内容才能使其运行,但是原理应该很清楚,这是您打算做什么?

struct CredentialsView: View {

  @State var signInSelected = true

  @State var firstName = ""
  @State var email = ""
  @State var password = ""
  @State var confirmPassword = ""

    var body: some View {
        VStack {
            Button("hola"){
                self.signInSelected.toggle()
            }

            VStack() {
                if signInSelected {
                    Text( "password")
                }
                else {
                   Text( "confirm Password")
                }
            }
            .padding(.top, signInSelected ? 110 : 50)
            Spacer()
        }
        .padding(.vertical, 40)
    }
}