简体   繁体   中英

Swift UI TextField onCommit called when lost focus (macOS)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM