1

I have SignUpView divided across two smaller views. Both of them connected in NavigationView of the first one. NavigationLink in the first view leads to the second one. I tried using iOS 15 @FocusState property wrappers to variables and .focused modifier to some Text Fields. All actions with those variables works properly in the first view, however when I go to the second view via NavigationLink it instantly returns to the first view with nothing there entered as if the view has been cleared out. This only happens when I'm using .focused modifier in the second view along with some actions connected with variables with @FocusState property wrappers.

The usage of @FocusState variables along with .focused modifier In the first View:

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

In the second View:

@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
    }
marc-medley
  • 8,931
  • 5
  • 60
  • 66
Vader20FF
  • 271
  • 2
  • 9
  • Try to use `.focused(, value:)` instead of two bools – Asperi Jan 10 '22 at 14:13
  • @Asperi You might be talking about `.focused(, equals: )`, right? Unfortunately introducing hashable enum with TextFields and then using the above version of .focused modifier (like in official Apple Developer Documentation) did not help. – Vader20FF Jan 10 '22 at 18:28

0 Answers0