This is taken from a completely fresh multiplatform SwiftUI project. I want to be able to make a custom view focusable, so I tried testing with a Text
view but it's not working. Isn't that the whole point of .focusable()
to declare an arbitrary view focusable? If that's the case, then why isn't it working in the below code:
import SwiftUI struct ContentView: View { @FocusState private var focusedItem: Optional<FocusedItem> var body: some View { VStack { Text("Test") .focusable(true) .focused($focusedItem, equals: .two) Button("Toggle Focus") { self.focusedItem=(self.focusedItem == nil) ? .two : nil DispatchQueue.main.asyncAfter(deadline: .now() + 3) { // Always prints `nil`. print(self.focusedItem) } } } } enum FocusedItem { case one, two func other() -> FocusedItem { switch self { case .one: return .two case .two: return .one } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }