【问题标题】:How to use textChange event in SwiftUI TextField Component?如何在 SwiftUI TextField 组件中使用 textChange 事件?
【发布时间】:2020-04-17 12:08:23
【问题描述】:

正如我们在 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 将在按下返回键时打印。

现在我正在输入 ABC

所以现在的问题是

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

【问题讨论】:

    标签: ios swift uitextfield swiftui textfield


    【解决方案1】:

    你可以用这个:

    import SwiftUI
    
    struct ContentView: View {
    
    @State var txt = ""
    
    var body: some View {
        TextField("Enter txt", text: Binding<String>(
            get: { self.txt },
            set: {
                self.txt = $0
                self.callMe(with: $0)
            }))
            .padding(20)
    }
    
    func callMe(with tx: String) {
        print("---> as you type \(tx)")
    }
    }
    

    【讨论】:

    • 很高兴我能帮上忙。请将答案标记为正确。
    猜你喜欢
    • 2012-07-03
    • 2012-04-02
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多