我有一个奇怪的案例。我在iOS 10中使用了四个文本字段。文本字段包含文本,但在选择另一个文本字段时它变为空白。我的班级是UITextFieldDelegate
这些文字字段。可能是什么问题?我附上了显示案例的图片。
当resignFirstResponder
时,文本字段(空闲超时)变空resignFirstResponder时,另一个文本字段(幻灯片超时)变空。
[![在此处输入图像说明] [2]] [2]
首先,我正在通过此功能重置文本字段:
func resetTextField(){
self.slideTimeOutTxtField.resignFirstResponder()
self.idelTimeOutTxtField.resignFirstResponder()
self.idelTimeOutTxtField.resignFirstResponder()
self.passwordTxtField.resignFirstResponder()
self.longPressDurationTxtField.resignFirstResponder()
self.slideTimeOutTxtField.layer.borderWidth = 0
self.idelTimeOutTxtField.layer.borderWidth = 0
self.passwordTxtField.layer.borderWidth = 0
self.longPressDurationTxtField.layer.borderWidth = 0
let slideTimeOut = DatabaseController.settings.last?.slideTimeOut
let idelTimeOut = DatabaseController.settings.last?.idleTimeOut
let password = DatabaseController.settings.last?.password
let longPressDuration = DatabaseController.settings.last?.longPressDuration
self.slideTimeOutTxtField.text = (slideTimeOut != nil ) ? ("\(String(describing: slideTimeOut!))") : ""
self.idelTimeOutTxtField.text = (idelTimeOut != nil) ? ("\(String(describing: idelTimeOut!))") : ""
self.passwordTxtField.text = (password != nil) ? password : ""
self.longPressDurationTxtField.text = (longPressDuration != nil) ? ("\(String(describing: longPressDuration!))") : ""
}
然后我通过这些函数实现它们:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
switch textField {
case self.idelTimeOutTxtField , self.slideTimeOutTxtField , self.longPressDurationTxtField :
if ((string.rangeOfCharacter(from: CharacterSet.decimalDigits)) != nil) || (string.isEmpty){ //isEmpty when delete on character
return true
}
else {
return false
}
default:
return true
}
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textField.layer.borderWidth = 0
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.okBtnAction(self.okBtn)
return true
}
我将TextFields放在另一个视图中,就像在图像TextFields are placed inside another View中一样,这会产生什么影响吗?
我上传了动态GIF:https://imgur.com/a/v3x4j