I'm working on an app in SwiftUI where I use multiple views. My app's settings view uses a custom UI with buttons for navigation instead of a List. I was previously using NavigationView, but due to depreciation, I switched to using NavigationStack. I'm unsure if I've implemented this correctly.
The hierarchy of my app is as follows:
Dashboard > App Settings > Account Settings > Change Information > Name/Email/Phone/Password/... > CHANGE Information View.
In the settings part of my app, the root is the "App Settings" view, which contains various other settings views like "Account Settings". Some views need to change programmatically after a function runs, so I can't use NavigationLink for my buttons.
Here's an example of my current structure:
Settings View:
Button(action: {
print("user pressed")
}, label: {
NavigationLink(destination: {
SettingsProfileView()
}, label: {
// UI code omitted for brevity
})
})
Account Settings View:
Button(action: {
print("Age Restriction Pressed")
withAnimation(.easeInOut(duration: 0.2)) {
}
}, label: {
NavigationLink(destination: {
AccountInfoSubView()
}, label: {
// UI code omitted for brevity
})
})
The problem I'm having is with the "Verify Current Password" view which verifies the user's current password before they can change it. After the user changes their password and clicks save, it's supposed to go back to the previous view. However, it seems like the .navigationDestination isn't recognizing that when isPresented is no longer true, it doesn't return to the previous view.
Verify Current Password View:
.navigationBarItems(trailing:
Button(action: {
isPresented = true
}) {
Text("Next")
.foregroundColor(UserValidate().isPasswordValid(textField) ? .orange : .gray)
}
.navigationDestination(isPresented: $isPresented, destination: {
PasswordChangeView(isPresented: $isPresented)
})
)
.preferredColorScheme(.dark)
.padding()
And finally, the "Change Account Password" view:
.navigationBarItems(trailing: Button(action: {
isPresented = false
}))
This last view isn't returning to the previous view, even though isPresented is now false. How can I properly structure my navigation so that my views behave as expected?