0

I have a ToolbarItemGroup with a navigation link and a button. With the current setup when I hit "Add" it goes to the AddNewTripView just fine but when I hit the back button (to return to the list view) it goes back to the previous view and then jumps right back to the AddNewTripView.

If I remove the Edit button it works fine. Is this a bug in SwiftUI or am I doing something wrong?

var body: some View {
        ScrollView { }
        .onAppear {
            tripListViewModel.getAllTrips()
        }
        .navigationTitle("Trips")
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItemGroup(placement: .navigationBarTrailing) {
                NavigationLink(destination: AddNewTripView(repo: repo)) {
                    Text("Add")
                }
                Button(action: { isEditing.toggle() }) {
                    Text(isEditing ? "Done" : "Delete")
                        .foregroundColor(.primary)
                }
            }
        }
}
OEZ
  • 55
  • 8
  • does it also happen if you skip the `.onAppear` ? – ChrisR Mar 22 '22 at 21:22
  • @ChrisR no it doesn't.... I was focused on the toolbar and didn't think of trying to remove that. So would that mean the problem is in `tripListViewModel.getAllTrips()`? – OEZ Mar 22 '22 at 21:50
  • When you navigate back from `AddNewTripView` the `.onAppear` will fire again and rebuild the view. I just don't understand why it then would trigger the `NavigationLink` again. You could try to move the `NavigationLink` out of the toolbar and activate it programmatically with `NavigationLink(selection: )` – ChrisR Mar 22 '22 at 21:56
  • Does this answer your question? [SwiftUI - Custom Navigation "Back" button, oddly jumping back and forth between views](https://stackoverflow.com/questions/59848678/swiftui-custom-navigation-back-button-oddly-jumping-back-and-forth-between) – Yrb Mar 22 '22 at 23:51
  • I will look into that option..... I just found that the save button on a different view is doing something similar. – OEZ Mar 23 '22 at 05:14

0 Answers0