如何在 SwiftUI TextField 组件中使用 textChange 事件?

How to use textChange event in SwiftUI TextField Component?

因为我们在 Swift

中使用了 UITextField

textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)

和 SwiftUI 中的 TextField 提供

TextField("", text: $input, onEditingChanged: { changed in  
        print("Changed")  
        self.output = "You are typing: " + self.input  
      }, onCommit: {  
        print("Commited")  
        self.output = "You typed: " + self.input  
      })

Changed 将在开始编辑时打印,Commited 将在 return 按键时打印。

现在我正在输入 ABC

所以现在的问题是

如果我想在按下 A 时调用任何函数或进程,我需要执行哪些步骤?

你可以使用这个:

import SwiftUI

struct ContentView: View {

@State var txt = ""

var body: some View {
    TextField("Enter txt", text: Binding<String>(
        get: { self.txt },
        set: {
            self.txt = [=10=]
            self.callMe(with: [=10=])
        }))
        .padding(20)
}

func callMe(with tx: String) {
    print("---> as you type \(tx)")
}
}