1

I am having the following text field in content view:

func doCommit(){
   print("\(self.status.textValue)")
}
var body : some View {
TextField("Your input:", text: self.$status.textValue, onCommit:{
                            self.doCommit()
                        }).lineLimit(1).focusable(true, onFocusChange: { focused in
                            print ("\(focused) changed")
                        })
 ... 
 }

When the text field got focus, and type return , the doCommit got called, which is what I want, but when I click on something else, and then click the text field and click somewhere else, the doCommit got called again, and the focusable onFocusChange is not call. I do not want the func been called when lost focus , but only when return/entry

user5616998
  • 49
  • 11
  • Please check this question https://stackoverflow.com/questions/56507839/swiftui-how-to-make-textfield-become-first-responder. Try to use introspect (in the answer given by Gregor Brandt), and use introspect – Carlos Maria Caraccia Nov 11 '20 at 08:38
  • This is a feature, built-in SwiftUI TextField behaviour. – Asperi Nov 11 '20 at 10:53
  • can the feature be disabled ? @Asperi – user5616998 Nov 12 '20 at 05:55
  • I am getting the compile error: Value of type 'SetFirstResponderTextField.Content' (aka '_ViewModifier_Content') has no member 'introspectTextField' with Gregor Brandt's answer, Xcode 12.1 , swift UI – user5616998 Nov 12 '20 at 06:05

1 Answers1

0

It appears onFocusChange only gets called when you use the tab key to iterate through different UI elements, not when you click into and away from the TextField

keehun
  • 41
  • 4