Use FocusState.
Here is a simple code example:
struct ContentView: View {
let textCase1 = 1
let textCase2 = 2
@State var text1: String = ""
@State var text2: String = ""
@FocusState private var focusedField: Int?
var body: some View {
VStack {
HStack {
Text(" TextField 1")
TextField("", text: $text1)
.border(Color.blue, width: 1)
.focused($focusedField, equals: textCase1)
.onSubmit {
focusedField = 2
}
}
HStack {
Text(" TextField 2")
TextField("", text: $text2)
.border(Color.red, width: 1)
.focused($focusedField, equals: textCase2)
.onSubmit {
focusedField = 1
}
}
Button {
focusedField = nil
print("No more focus")
} label: {
Text("Do something else")
}
}
}
}