Open onmyway133 opened 1 year ago
Use underscore _focus we get access to underlying FocusState object, but underscore _ is private to a View hence can't be used in extension
_focus
FocusState
_
If we want to pass FocusState to another View or in extension, we can pass its Binding
Binding
enum FocusElement: Hashable { case name case email } struct ParentView: View { @FocusState var focus: FocusElement? var body: some View { ChildView1(focus: _focus) ChildView2(focus: $focus) } } struct ChildView1: View { @FocusState var focus: FocusElement? var body: some View { TextField("Name", text: .constant("")) .focused($focus, equals: .name) } } struct ChildView2: View { var focus: FocusState<FocusElement?>.Binding var body: some View { TextField("Email", text: .constant("")) .focused(focus, equals: .name) .onAppear { focus.wrappedValue = .email } } }
Use underscore
_focus
we get access to underlyingFocusState
object, but underscore_
is private to a View hence can't be used in extensionIf we want to pass FocusState to another View or in extension, we can pass its
Binding