Swiftui TextField";简介;轻触键盘快捷界面(iOS 14)

Swiftui TextField";简介;轻触键盘快捷界面(iOS 14),swiftui,keyboard,textfield,ios14,Swiftui,Keyboard,Textfield,Ios14,当您使用TextField在SwiftUI中按下intro和键盘时,是否尝试过处理事件 TextField("type your name here", text: $yourName) 我如何知道用户何时按“介绍”按钮关闭键盘 非常感谢您 声明文本字段后,添加一个闭包并用以下代码填充:UIApplication.shared.keyWindow?.endEditing(true) 所以总体上看 TextField($yourText){ UIApplication

当您使用
TextField
SwiftUI
中按下
intro
键盘
时,是否尝试过处理事件

TextField("type your name here", text: $yourName)
我如何知道用户何时按“介绍”按钮关闭键盘


非常感谢您


声明
文本字段后,添加一个闭包并用以下代码填充:
UIApplication.shared.keyWindow?.endEditing(true)

所以总体上看

TextField($yourText){
    UIApplication.shared.keyWindow?.endEditing(true)
}

这段视频在8点左右解释了这一点


您可以使用
TextField
中的
onCommit
参数。以下是一个例子:

TextField("type your name here", text: $yourName, onCommit: {
    print("return key pressed")
})

从文件中:

/// [...]
///   - onCommit: An action to perform when the user performs an action
///     (for example, when the user presses the Return key) while the text
///     field has focus.
public init(_ titleKey: LocalizedStringKey, text: Binding<String>, onEditingChanged: @escaping (Bool) -> Void = { _ in }, onCommit: @escaping () -> Void = {})
//[…]
///-onCommit:用户执行操作时要执行的操作
///(例如,当用户按下返回键时)文本
///场有焦点。
public init(uTitleKey:LocalizedStringKey,text:Binding,onEditingChanged:@escaping(Bool)->Void={in},onCommit:@escaping()->Void={})

onCommit允许您在用户按“返回键”时进行处理,这正是我要找的。。。不管怎样,我真的很感谢你的回答,非常感谢!