Fixed that focusTextField may gets called during view update resulting in 'undefined behavior'

This commit is contained in:
dankito 2020-09-07 15:01:13 +02:00
parent 0baa74484b
commit 3d11614921
1 changed files with 7 additions and 5 deletions

View File

@ -91,10 +91,12 @@ struct UIKitTextField: UIViewRepresentable {
uiView.text = text
if focusTextField {
uiView.focus()
DispatchQueue.main.async {
self.focusTextField = false // reset value so that it can be set again (otherwise it may never gets resetted and then updateUIView() requests focus even though already another view got the focus in the meantime)
DispatchQueue.main.async { // in very few cases focusTextField gets called during view update resulting in 'undefined behavior' -> async() fixes this
uiView.focus()
DispatchQueue.main.async {
self.focusTextField = false // reset value so that it can be set again (otherwise it may never gets resetted and then updateUIView() requests focus even though already another view got the focus in the meantime)
}
}
}
}
@ -192,7 +194,7 @@ struct UIKitTextField: UIViewRepresentable {
let nextView = textField.superview?.superview?.superview?.viewWithTag(nextViewTag)
?? textField.superview?.superview?.superview?.superview?.superview?.viewWithTag(nextViewTag) // for text fields in Lists (tables)
didHandleReturnKey = nextView?.focus() ?? false
}