'Is there a way to assign an enum value not associated with any TextField to FocusState value?
Problem
Change of FocusState value to an enum value is not applied.
Attempt
I tried using the focused(_:equals:) method to an EmptyView but it didn't work.
Environment
Xcode 13.2.1
iOS 15
Code
struct FocusStateTestView: View {
private enum FocusStateField: Hashable{
case firstField
case secondField
case noneField
}
@State private var firstValue = ""
@State private var secondValue = ""
@FocusState private var focusState: FocusStateField?
var body: some View {
Form {
TextField("First", text: $firstValue)
.focused($focusState, equals: .firstField)
TextField("Second", text: $secondValue)
.focused($focusState, equals: .secondField)
// I have tried below...
EmptyView()
.focused($focusState, equals: .noneField)
Button("focus on First"){
focusState = .firstField
}
Button("focus on Second"){
focusState = .secondField
}
Button("focus on None"){
// not work...
focusState = .noneField
}
Button("Close"){
focusState = nil
}
}
.onChange(of: focusState) { newValue in
print("focus \(newValue.debugDescription)")
}
}
}
How can I assign .noneField to focusState?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
