Search code examples
swiftuipopuponappear

Render parent view again after pop up SwiftUI


I have a main view with a button that leads to a setting view. Both views have access to a property in appstorage. This property gets changed in the setting view and it is used in the main view on appear to compute and display values.

Does changing the property in appstorage re-render the main view ? In other words will onAppear be called again ?

Note : To pop up the second view I would like to use a boolean value, when it is true I display the setting view in a ZStack after the main view so it appears on top


Solution

  • if your main view relies on a property stored in @AppStorage, it should update automatically when that property changes, because @AppStorage is a property wrapper that automatically observes the UserDefaults value for changes, triggering a view update when the value changes.

    if you change a value in @AppStorage from a settings view that is presented on top of your main view (e.g using a ZStack), the main view itself will update because of the @AppStorage property change, but onAppear will not be called again when the settings view is dismissed, because the main view was never removed from the view hierarchy

    you can use a different approach. Instead of depends on onAppear, you might consider using a custom publisher or an observable object that updates when the settings change, and observing that in your main view.

    Note: If you are unable to implement the suggested solution. Update your question with sample example I will help you on that.