Learn Courses My Dashboard

How to return to the 1st view

From the home view I have a set of sub-views that user will go by using a button in each view using a navigationBarItems(trailing: NavigationLink approach.
When the user reaches the last sub-view there is a button for storing the data. When the button is used I would like the view to return to 1st home view.
The self.presentationMode.wrappedValue.dismiss() just brings back to the previous view.

use navigation link with tag and selection parameters. and bind the last page with the selection and set it to nil

Thanks for the reply. Meanwhile I have trouble implementing your solution.
My Home view has:
TabView (selection: $selectedTab) {
howDoYouFeelView().tabItem{
VStack {
Image(systemName: “thermometer.sun.fill”)
Text(“How do you feel?”).bold()
}
}.tag(Constants.howDoYouFeelTab)
Then howDoYouFeelView has a .navigationBarItems(trailing: NavigationLink (destination: emotionsView() …
Then emotionsView has a .navigationBarItems(trailing: NavigationLink (destination: summaryView()

In summaryView I have a Button to store some data and I want to return to home view.

Not sure what binding is required.

@State var selectedLink: Int?

NavigationLink(tag: 1, selection: $selectedLink) {
                    LastPage(selectedLink: $selectedLink)
                } label: {
                    VStack {
                        Image(systemName: "home")
                        Text("Go to home")
                       
                    }
                    .padding()
// on the last page use binding @Binding selectedLink: Int?

// and on the button action after storing your data also set selectedLink = nil

Hello alaber! I had the same problem. I used navigation links with tabs. I typed all those navigation links in the view where the NavigationView was (very important, otherwise it would not work) but without a label. Then I bound their selectedLink to a variable in an EnvironmentObject. I can change the value of this variable from any view. To return to the first view, you just have to set that variable to nil. You can return to the first screen like that, no matter in which view you are. This solution has worked really well for me.

Thank you guys for your replies.
My issue was that my Home View is using TabView not NavigationLink.
So I created a WelcomeView that just allows via a NavigationLink (with a “tag: 0, selection: $model.currentViewSelected” to go to the HomeView and from my last Navigation View in the Button that allows the recording of the data entered by the user I set model.currentViewSelected to nil and I happily return to the Welcome View.