0

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?

1
  • 1
    navigationBarItems Was deprecated 2 or 3 os versions ago there is no reason to use it with the newest tools such as NavigationStack and navigationDestination Commented May 31, 2023 at 10:02

1 Answer 1

0

I think that you are using the old bad way to set destination on your navigation flow.

  • First: Define your navigationLink and for each link set the value which gonna be as a key. (you can write an enum for that)
  • Second: In a wrapper put your navigation destination read that enum and return the view you want to show
NavigationStack {
    List {
        NavigationLink("Mint", value: Color.mint)
        NavigationLink("Pink", value: Color.pink)
        NavigationLink("Teal", value: Color.teal)
    }
    .navigationDestination(for: Color.self) { color in
        ColorDetail(color: color)
    }
    .navigationTitle("Colors")

I will share with you a post and a video where is explained better.

https://www.avanderlee.com/swiftui/navigationlink-programmatically-binding/

https://www.youtube.com/watch?v=piAiy5vlC9k&t=3566s&ab_channel=KarinPrater

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.