简体   繁体   English

Swift 失去焦点时调用 UI TextField onCommit (macOS)

[英]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.当文本字段获得焦点并键入 return 时,doCommit 被调用,这是我想要的,但是当我单击其他内容,然后单击文本字段并单击其他地方时,doCommit 再次被调用,并且可聚焦onFocusChange 不是调用。 I do not want the func been called when lost focus, but only when return/entry我不希望在失去焦点时调用 func,但仅在返回/输入时调用

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只有当您使用 Tab 键遍历不同的 UI 元素时才会调用onFocusChange ,而不是当您单击进入和离开TextField

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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