我正在我的项目中进行自动完成,我想检测textfieldDidChange值的时间并在此之后调用一个方法(链接到API)500MS. 我希望它足够清楚 谢谢您帮忙 ! 首先确保使您的类成为TextField的委托: clas
我希望它足够清楚
谢谢您帮忙 !
class yourClass: UIViewController, UITextFieldDelegate{
//...
}
然后在viewDidLoad()中:
textField.delegate = self numbersTextField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
之后,您可以使用计时器,如下所示:
var timer = Timer()
var count = 0
func textFieldDidChange(textField: UITextField){
timer.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.timerFunc), userInfo: nil, repeats: true)
}
func timerFunc(){
count += 1
if count == 5{
timer.invalidate()
// do your things here down here, I assumed that your method is called autocomplete
autocomplete()
}
}
希望能帮助到你!
