From 3d116149216be1a6bc7f1b31a675bab4cdaf7dcb Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 7 Sep 2020 15:01:13 +0200 Subject: [PATCH] Fixed that focusTextField may gets called during view update resulting in 'undefined behavior' --- .../BankingiOSApp/ui/UIKitTextField.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift index 8bd0f018..ba210df8 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift @@ -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 }