2 years ago

#33182

test-img

Vader20FF

Using @FocusState variables in NavigationLink View makes this view return back instantly to the previous view

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
    }

ios

swiftui

textfield

navigationview

property-wrapper

0 Answers

Your Answer

Accepted video resources