'@State variable getting reset for no reason [ios 15 SwiftUI]
I am using a @State variable to store a tag of some navigation Links. But in ios 15, I notice that whenever I go to background, the state variable is getting reset, which causes that when I go back into the app, the view get popped for no reason.
@State private var userNavigateSelection: String? = nil
NavigationLink(destination: TestView(), tag: TestTag, selection: $userNavigateSelection) { EmptyView() }
I used a onChange method to monitor the changing of "userNavigateSelection", everytime I go into background. "userNavigationSelection" will be changed to nil. I have no idea why.
Solution 1:[1]
Perhaps your view containing @State variable is removed from view hierarchy after coming back to foreground. Try moving this variable higher in hierarchy and pass to view containing NavigationLink with @Binding. Check: https://www.ralfebert.com/swiftui/state-lifetime/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Becod |
