Discover how to make `UIViewRepresentable` listen to changes in `ObservedObject` in SwiftUI for iOS 14, ensuring your views update correctly.
---
This video is based on the question https://stackoverflow.com/q/65046795/ asked by the user 'harunaga' ( https://stackoverflow.com/u/10441916/ ) and on the answer https://stackoverflow.com/a/65047308/ provided by the user 'Patrick Sturm' ( https://stackoverflow.com/u/7121596/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: iOS 14 SwiftUI UIViewRepresentable updateUIView doesn't detect ObservedObject changing?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Solving iOS 14 UIViewRepresentable Update Issues with ObservedObject in SwiftUI
When developing applications using SwiftUI, you may encounter situations where your UI does not update as expected when an @ ObservedObject changes. This can be particularly apparent in iOS 14, where the interaction between UIViewRepresentable and ObservedObject can lead to some quirks. In this post, we’ll dive into a specific problem you might face and provide a clear solution that will help ensure your views update as intended.
The Problem
You have implemented a UITextView using UIViewRepresentable, and it was working fine in iOS 13. You expected that when the color associated with your ObservedObject changes, the updateUIView method would be called to update the text color accordingly. However, on iOS 14, this method isn't being called, and your UI fails to reflect the changes.
Here’s an example of how you're currently using UIViewRepresentable with ObservedObject:
[[See Video to Reveal this Text or Code Snippet]]
As you can see, when using a button to change the color of the text, updateUIView should be triggered. However, in iOS 14, it’s not happening.
The Solution
To resolve this issue, you need to make CustomTextView listen for changes in TmpTextViewModel correctly. The key is to declare the tmpTextVM property as @ ObservedObject instead of a simple variable. This adjustment allows SwiftUI to automatically observe changes in the model and call the updateUIView method when the ObservedObject changes.
Step-by-Step Fix
Modify the CustomTextView Declaration: Change the declaration of tmpTextVM to be an @ ObservedObject.
[[See Video to Reveal this Text or Code Snippet]]
Retain the Binding: Ensure that anywhere you're using CustomTextView, you're passing an instance of TmpTextViewModel that is marked with @ ObservedObject in its parent view, just like you did with ContentView:
[[See Video to Reveal this Text or Code Snippet]]
Why This Works
By using @ ObservedObject, you not only inform SwiftUI to watch for changes in tmpTextVM, but you also ensure that any updates to properties that are marked with @ Published within TmpTextViewModel (like color) will automatically trigger the UI to refresh. This is an essential part of leveraging the benefits of reactive programming in SwiftUI.
Conclusion
Ensuring that your UIViewRepresentable responds to changes in @ ObservedObject is crucial for a responsive UI in iOS. By declaring your observed objects correctly, you’ll keep your interfaces in sync with the underlying data model, allowing for smooth and predictable interactions in your app. With this solution, you can confidently handle updates within your SwiftUI applications on iOS 14 and beyond.
Now, whenever you change the color of your text through the button, updateUIView will be invoked appropriately, and the UI will reflect the new state, providing users with a sublime experience.
Информация по комментариям в разработке