Learn Courses My Dashboard

NavigationLink isActive is already deprecated - cannot go back to root view programmatically

Hi

The background is I have 2 Views

  • Parent View → Detail View
  • the Detail View is defined as a function that return some view inside the parent view class (not in separate file)
  • Parent View has a NavigationStack that has Navigation Link inside which will lead you to the detail view
  • in detail view, automatically there is back where you can go back to parent view
  • in detail view, I added a save button, to which I want to go back to parent view when save is done

" And there came my PROBLEM" I could not switch the view back to parent programmatically

before there was isPresentedBinding/isActive but it seems its already deprecated

struct GenericManageView: View {
   ...

  var body: some View {

    NavigationStack() {
      List(items, id: \.self) { item in
               NavigationLink {
                    editItemView(lItem: item)
                } label: {
                    listItemView(lItem: item)
                }
      }

    }

  }// end of view body

  func editItemView(lItem: Item) -> some View {

    // display a save button here that will switch view back to body when successfully saved core data

  }

}

The solution is to add an @Environment object to your DetailView before the body property like this:

@Environment(\.dismiss) var dismiss

In your save button action code, do what ever you have to do in order to save your data and then add the following to dismiss the view and return you to the Parent View.

dismiss()

Hi Chris,

It works.

I guess I have no choice but to separate the DetailView to a separate structure.

My previous implementation was I put the definition of detail view inside the struct of its parent view.

Again, thanks