所以我有一个 UIViewRepresentable 包装了一个 UITextField,并且在 textFieldshouldChangeCharactersInRange
方法期间收到此错误:
struct MyTextField: UIViewRepresentable {
class Coordinator: NSObject, UITextFieldDelegate, ObservableObject {
@Binding var text: String
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let currentValue = textField.text as NSString? {
let proposedValue = currentValue.replacingCharacters(in: range, with: string)
text = proposedValue // Warning is thrown from here
}
return true
}
}
...
在此处更新绑定(bind)而不触发此警告的正确方法是什么?
最佳答案
更新下一个偶数循环,例如
DispatchQueue.main.async {
text = proposedValue
}
关于ios - 警告 : "Modifying state during view update, this will cause undefined behavior." in UIViewRepresentable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71914728/