0

我将 SignUpView 分为两个较小的视图。他们两个都连接NavigationView到第一个。NavigationLink在第一个视图中导致第二个。我尝试使用 iOS 15@FocusedState属性包装器对变量和.focused修饰符使用一些文本字段。具有这些变量的所有操作在第一个视图中都可以正常工作,但是当我通过它转到第二个视图时,NavigationLink它会立即返回到第一个视图,没有任何输入,就好像视图已被清除一样。这只发生在我.focused在第二个视图中使用修饰符以及一些与具有@FocusedState属性包装器的变量相关的操作时。

在第一个视图中@FocusedState variables的用法:.focused modifier

@State var email: String = ""
@State var password: String = ""

@FocusState private var isEmailTextFieldFocused: Bool
@FocusState private var isPasswordTextFieldFocused: Bool

TextField("E-mail", text: $email)
    .focused($isEmailTextFieldFocused)
    .onSubmit {
        isEmailTextFieldFocused = false
        isPasswordTextFieldFocused = true
    }

SecureField("Password", text: $password)
    .focused($isPasswordTextFieldFocused)
    .onSubmit {
        isPasswordTextFieldFocused = false
    }

在第二个视图中:

@State private var email: String = ""
@State private var password: String = ""
@State private var repeatedPassword: String = ""

@FocusState private var isEmailTextFieldFocused: Bool
@FocusState private var isPasswordTextFieldFocused: Bool
@FocusState private var isRepeatedPasswordTextFieldFocused: Bool

TextField("E-mail", text: $email, onCommit: {
    Task {
        self.emailTaken = try await signUpViewModel.checkEmailDuplicate(email: email)
    }
})
    .focused($isEmailTextFieldFocused)
    .onSubmit {
        isEmailTextFieldFocused = false
        isPasswordTextFieldFocused = true
        isRepeatedPasswordTextFieldFocused = false
    }

SecureField("Password", text: $password)
    .focused($isPasswordTextFieldFocused)
    .onSubmit {
        isEmailTextFieldFocused = false
        isPasswordTextFieldFocused = false
        isRepeatedPasswordTextFieldFocused = true
    }

SecureField("Confirm Password", text: $repeatedPassword)
    .disableAutocorrection(true)
    .autocapitalization(.none)
    .focused($isRepeatedPasswordTextFieldFocused)
    .onSubmit {
        isEmailTextFieldFocused = false
        isPasswordTextFieldFocused = false
        isRepeatedPasswordTextFieldFocused = false
    }
4

0 回答 0