Replies: 1 comment 9 replies
-
Hey @seanmrich! I'm not 100% sure about what's going on here. @available(iOS 14, macOS 11, tvOS 14, watchOS 7, *)
private struct _Bind<ModelValue: _Bindable, ViewValue: _Bindable>: ViewModifier
where ModelValue.Value == ViewValue.Value, ModelValue.Value: Equatable {
let modelValue: ModelValue
let viewValue: ViewValue
@State var hasAppeared = false
func body(content: Content) -> some View {
content
.onAppear {
guard !self.hasAppeared else { return }
self.hasAppeared = true
guard self.viewValue.wrappedValue != self.modelValue.wrappedValue else { return }
self.viewValue.wrappedValue = self.modelValue.wrappedValue
}
.onChange(of: self.modelValue.wrappedValue) {
// guard self.viewValue.wrappedValue != $0
// else { return }
self.viewValue.wrappedValue = $0
}
.onChange(of: self.viewValue.wrappedValue) {
guard self.modelValue.wrappedValue != $0
else { return }
self.modelValue.wrappedValue = $0
}
}
} |
Beta Was this translation helpful? Give feedback.
-
I tried using the
.bind
modifier from swiftui-navigation on a custom view using.focusable()
, but it behaves erratically. Here's a simple setup to demonstrate:To see the behavior, you must have keyboard navigation turned on (System Settings→Keyboard→Keyboard navigation). When launched the view will have focus on the circle and the model indicates its focus is
circle
. When you hit TAB, the focus moves to the title, but the model still thinks the focus is on the circle. Any further TABs have no effect--well, not exactly. There's a flash of the focus ring when you hit TAB. Presumably it's trying to move to the next key view, but is immediately reset back to the title.If you replace the
bind
modifier with something along the lines of the focus state episode from last year,you get a better result:
Replace
.bind
with.synchronize
and now you get the expected behavior. Focus moves back and forth between the title field and the circle. I tried replicating thebind
modifier and stripping it down to the same logic as in thesynchronize
modifier, but I get no improvement. Somehow, wrapping the logic in aViewModifier
changes the behavior. Can anyone help me resolve this issue? If it makes a difference, I'm using Xcode 14.1 on Ventura 13.0.1.Parenthetically, I don't understand how the
synchronize
modifier compiles. TheFocusState
property wrapper requires itsValue
to beHashable
, but the modifier only requiresEquatable
. The sameEquatable
requirement is in thebind
modifier. How does that compile withoutHashable
conformance?Beta Was this translation helpful? Give feedback.
All reactions