I found a weird problem when I use @FocusState
.
I need to compare the value of @FocusState
and display different View
. But I found that compare @FocusState
will trigger a change of it's value.
The code **** will display two list of buttons. When you select the 2 button in the first list, the right button will display. When I try to move to the right button, the focus will jump to the 1 button in the first list. The reason is focused
was changed to 1 when I try to jump to the right button(the value of focused
will be nil
and then 1
).
And if I remove the compare code(if focused == 2
), it will work fine.
I am currently using Xcode beta 6. Not sure if it is a bug in beta version.
struct Demo: View {
@FocusState var focused: Int?
var body: some View {
HStack {
List {
ForEach((1...10), id: \.self) { number in
Button {
} label: {
Text("\(number)")
}
.focused($focused, equals: number)
}
}
List {
if focused == 2 {
Button {
} label: {
Text("Button")
}
}
}
}
}
}