I am still kinda a newbie in programming. I have a problem how to deal with FocusState in my project.
My parent View has an enum created on top:
enum RegistrationFocus: Hashable { case username, password, confirmPassword, nameAndSurname, email, confirmEmail, phoneNumber }
And i have a focusState variable inside my struct
@FocusState var focus: RegistrationFocus?
My childview is in a VStack to display all views. (only wrote 3 down there for now)
PersonalUserDetailsRow(title: "UserName", text: $registerModel.username, textContentType: .username, focus: $focus, focusfield: .username) PersonalUserDetailsRow(title: "Password", text: $registerModel.password, textContentType: .newPassword, focus: $focus, focusfield: .password) PersonalUserDetailsRow(title: "Confirm Password", text: $registerModel.confirmPassword, textContentType: .confirmPassword focus: $focus, focusfield: .confirmPassword)
And my ChildView looks like this:
`struct PersonalUserDetailsRow: View {
var title: String
@Binding var text: String
let textContentType: UITextContentType
var focus: FocusState<RegistrationFocus?>.Binding
@State var focusField: RegistrationFocus?
var body: some View {
if textContentType == .newPassword {
SecureField(title, text: $text)
.padding(.top, 15)
.disableAutocorrection(true)
.textContentType(textContentType)
.focused(focus.projectedValue, equals: focusField)
Divider() .overlay(Color.black)`
So my child view does contain var focus: FocusState<RegistrationFocus?>.Binding and State focusField: RegistrationFocus?
But i have no clue how to use those 2.
I want to go to the next textField when the user tappes the return key on keyboard.
I have seen many examples where people use 2 textfields in Parent view and i understand those examples and how to move around textFields when using .focused(, equals:).
But i cant find any examples how to do this with a child View, specialy with this .focused(focus.projectedValue, equals: focusField) if its even correct.
How should i do this so it will work?
And i keep geting this error: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions. with the above code
Any help appreciated. I suck at explaining, so hope people understand what i am asking .
Thank you.
I tried both of these :
var focus: FocusState<RegistrationFocus?>.Binding @State var focusField: RegistrationFocus?
@binding var focusField doesnt work.
And i dont really want to copy paste the textField and Divider() from child view 7 times in my parents view. It will be just to much copy pasting. I want to learn how to use the custom view with FocusState in my parents view.
Anze Slapar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.