hat*_*yte 6 uitextfield ios ios13
在 iOS 13 中,shouldChangeCharactersIn通过 实现时UITextfieldDelegate,应用程序在使用滑动键盘时崩溃。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text as NSString? {
let txtAfterUpdate = text.replacingCharacters(in: range, with: string)
textField.text = txtAfterUpdate
}
return false
}
Run Code Online (Sandbox Code Playgroud)
这是苹果的bug吗?
我能够重现这一点 - 如果您在滑动输入期间改变 UITextField 上的文本状态-并且仅在滑动输入期间,它会尝试重新插入滑动的内容(即使您返回 false),这会重新触发您的委托事件,开始递归循环。
这有点像黑客,但你可以用类似的东西抓住它
private var lastEntry: String?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.count > 1 && string == lastEntry { // implies we're swiping or pasting
print("Caught unwanted recursion")
return
}
lastEntry = string
if let text = textField.text as NSString? {
let txtAfterUpdate = text.replacingCharacters(in: range, with: string)
textField.text = txtAfterUpdate
}
return false
}
Run Code Online (Sandbox Code Playgroud)
它会阻止用户连续两次粘贴/滑动相同的东西,但至少它会让他们在 Apple 解决问题时滑动。
| 归档时间: |
|
| 查看次数: |
895 次 |
| 最近记录: |