如何在用户点击按钮时清除输入的文本字段?

-2 objective-c uitextfield ios swift3

我正在处理注册表单,在用户点击按钮后填写多个文本字段时,文本字段应该清除.

PPL*_*PPL 5

如果您不想使用UITextField集合,可以使用它,

@IBAction func buttontapped(_ sender: Any) {
    self.view.subviews.forEach({ item in
        if item.isKind(of: UITextField.self) {
            let txtItem = item as! UITextField
            txtItem.text = ""
        }
    })
}
Run Code Online (Sandbox Code Playgroud)

更新 @vacawama建议,

@IBAction func buttontapped(_ sender: Any) {
    for case let txtItem as UITextField in self.view.subviews {
        txtItem.text = ""
    }
}
Run Code Online (Sandbox Code Playgroud)

假设:所有文本字段都是self.view直接的子视图.如果textfields是其他的子视图customview,你应该使用它customview而不是self.view